Jump to content

How do I use animation.normalizedTime?


Recommended Posts

I am very new to writing plugins and I am trying to make it so that I can control an animation linked to my part. I have spent ages searching through old posts and such but still cant find an answer. Below is my code and I have highlighted the parts where I am trying to control the animation to hopefully make it easier to understand. Thanks


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace BalloonModule
{
public class BalloonModule : PartModule
{
[B][COLOR="#000080"][KSPField(isPersistant = false)]
public string inflateAnimation = "inflate";

public Animation anim;[/COLOR][/B]

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

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

public float balloonLift = 0;
public bool isInflated = false;

public override void OnStart(StartState state)
{
Debug.Log("Weather Balloons Mod Loaded!");
[B]anim = this.vessel.GetComponent(inflateAnimation);[/B]
}

public override void OnUpdate()
{
Debug.Log("Balloon Lift: " + balloonLift);
if(isInflated)
{
if (balloonLift < maxBalloonLift)
{
balloonLift += 0.01f * maxBalloonLift;
if (balloonLift > maxBalloonLift - 0.01) balloonLift = maxBalloonLift;
[B][COLOR="#000080"]anim.normalizedTime = (balloonLift/maxBalloonLift)/2;[/COLOR][/B]

}
float pressureCountdown = (float)FlightGlobals.getStaticPressure() - burstAtmoPressure;
[B][COLOR="#000080"]float animTime = 0.5f -(0.005f*pressureCountdown);
anim.normalizedTime = animTime;[/COLOR][/B]


this.rigidbody.AddForce(vessel.upAxis * balloonLift);
if (FlightGlobals.getStaticPressure() <= burstAtmoPressure)
{
Debug.Log("Balloon has Burst!");
balloonLift = balloonLift * 0.2f;
deflateBalloon();
}
}
else if (!isInflated && balloonLift > 0)
{
balloonLift -= 0.01f * maxBalloonLift;
if (balloonLift < 0.01) balloonLift = 0;
}
}

[KSPEvent(active = true, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Inflate Balloon")]
public void inflateBalloon()
{
if (FlightGlobals.getStaticPressure() > 0)
{
Debug.Log("Inflating Balloon!");
isInflated = true;
Events["inflateBalloon"].active = false;
Events["deflateBalloon"].active = true;
}
else
{
ScreenMessages.PostScreenMessage("Cannot inflate balloon in vacuum", 3, ScreenMessageStyle.UPPER_CENTER);
}
}

[KSPEvent(active = false, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiName = "Deflate Balloon")]
public void deflateBalloon()
{
Debug.Log("Deflating Balloon!");
Events["deflateBalloon"].active = false;
isInflated = false;
}
}
}

Edited by JoePatrick1
Link to comment
Share on other sites

Well first, do *not* use OnUpdate for physics. You are going to break stuff Physics go in FixedUpdate. And don't use OnUpdate or OnFixedUpdate actually, just use Update and FixedUpdate, else your methods might not be called.

Second, you need to make sure your animation is enabled, that it's speed is correct, and you must also call Animation.Play(animationName)

Link to comment
Share on other sites

Well first, do *not* use OnUpdate for physics. You are going to break stuff Physics go in FixedUpdate. And don't use OnUpdate or OnFixedUpdate actually, just use Update and FixedUpdate, else your methods might not be called.

Second, you need to make sure your animation is enabled, that it's speed is correct, and you must also call Animation.Play(animationName)

Thanks, it now works!

I tried changing 'OnUpdate()' to 'OnFixedUpdate()' but it doesn't seem to run as nothing happens. Am I missing something?

Link to comment
Share on other sites

That.

oh, thanks.

Although when I type 'public override void FixedUpdate()' it says no suitable method found to override so should it just be 'public void FixedUpdate()'

edit: tried it and it seems to work - let me know if I it's wrong though

Edited by JoePatrick1
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...