Jump to content

Change wing lift dependant on animation (solved, thank you)


Recommended Posts

I have an animated wing that deploys itself, but I have come across the unfortunate issue where stock KSP will only register the wings lift values from its initial position, meaning that when the wings are folded and stowed away, they still behave as though they're fully deployed.

I figure that one way around this is to link the lift values to the animation of the object (so in short, something like Lift = Frame * x).

I had to borrow from other plugins to figure out what I need, and I think I am very close to getting this working, but right now its just not doing anything.

This is what I have so far:

using UnityEngine;

namespace Animate2Variable

{

public class AnimatedWing : PartModule

{

[KSPField(guiActive = true, guiActiveEditor = false,

guiName = "deflectionLiftCoeff", isPersistant = false)]

public float deflectionLiftCoeff;

public void FixedUpdate()

{

if (part.Modules.Contains("ModuleControlSurface"))

{

deflectionLiftCoeff = animation["Deploy"].time ;

((ModuleLiftingSurface)part.Modules["ModuleControlSurface"]).deflectionLiftCoeff = deflectionLiftCoeff;

}

}

}

}

Edited by electronicfox
Link to comment
Share on other sites

Ok I got this far, but now my anim(Anim).normalizedTime throws up 'is a 'field' but is used like a 'method'.

namespace Animate2Variable

{

public class AnimatedWing : PartModule

{

public Animation anim;

[KSPField(isPersistant = false)]

public string Anim = "";

[KSPField(isPersistant = false)]

public float LiftMultiply = 0;

[KSPField(guiActive = true, guiActiveEditor = false,

guiName = "deflectionLiftCoeff", isPersistant = false)]

public float deflectionLiftCoeff;

public void FixedUpdate()

{

anim = part.FindModelAnimators(Anim)[0];

if (part.Modules.Contains("ModuleControlSurface"))

{

deflectionLiftCoeff = anim(Anim).normalizedTime;

((ModuleControlSurface)part.Modules["ModuleControlSurface"]).deflectionLiftCoeff = deflectionLiftCoeff;

}

}

}

}

Link to comment
Share on other sites

Because () are not [].

deflectionLiftCoeff = anim[Anim].normalizedTime;

Here is a cleaned up version. Of course you ll have to do more than just setting the current value of the anim time but I guess you already know that.



using UnityEngine;

namespace Animate2Variable
{
public class AnimatedWing : PartModule
{
[KSPField(isPersistant = false)]
public string animationName = "";


[KSPField(isPersistant = false)]
public float LiftMultiply = 0;

[KSPField(guiActive = true, guiActiveEditor = false,guiName = "deflectionLiftCoeff", isPersistant = false)]
public float deflectionLiftCoeff;

private ModuleControlSurface liftingSurface;

private Animation anim;

public override void OnStart(PartModule.StartState state)
{
anim = part.FindModelAnimators(animationName)[0];

liftingSurface = part.FindModuleImplementing<ModuleControlSurface>();
}

public void FixedUpdate()
{
if (anim == null || liftingSurface == null)
return;

deflectionLiftCoeff = anim[animationName].normalizedTime;

liftingSurface.deflectionLiftCoeff = deflectionLiftCoeff;
}
}
}

Link to comment
Share on other sites

Because () are not [].

deflectionLiftCoeff = anim[Anim].normalizedTime;

Here is a cleaned up version. Of course you ll have to do more than just setting the current value of the anim time but I guess you already know that.



using UnityEngine;

namespace Animate2Variable
{
public class AnimatedWing : PartModule
{
[KSPField(isPersistant = false)]
public string animationName = "";


[KSPField(isPersistant = false)]
public float LiftMultiply = 0;

[KSPField(guiActive = true, guiActiveEditor = false,guiName = "deflectionLiftCoeff", isPersistant = false)]
public float deflectionLiftCoeff;

private ModuleControlSurface liftingSurface;

private Animation anim;

public override void OnStart(PartModule.StartState state)
{
anim = part.FindModelAnimators(animationName)[0];

liftingSurface = part.FindModuleImplementing<ModuleControlSurface>();
}

public void FixedUpdate()
{
if (anim == null || liftingSurface == null)
return;

deflectionLiftCoeff = anim[animationName].normalizedTime;

liftingSurface.deflectionLiftCoeff = deflectionLiftCoeff;
}
}
}

Thank you! I knew it would be something really simple.

I already have another value editable in the cfg that will shrink down the animation time value to a reasonable amount for the final draft of this code.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...