Jump to content

How to animate a part with multiple animations?


Recommended Posts

I made a part with three animations:

- Heating up

- Working animation

- Lights

SeriousNeighboringAmericanriverotter.gif

I'd like to group the first two animations and run them in sequence when a "turn on" event occurs:

- Heat up (till animation finishes)

- Working (loop)

and then animator should loop the second animation until the "turn off" event occurs after which it should end both animations simultaneously in this fashion:

- Heat up (backwards animation)

- Working (stop looping)

The key is blending them together.

For the "lights" animation I wish it didn't interrupt the "working" animation.

I'm searching for the code which my friend could implement into his plugin.

Is there any mod that has a similar animation code? Where should we look?

Edited by Enceos
Link to comment
Share on other sites

You could have a startup coroutine and a shutdown coroutine. (using System.Collections)

You might need another animation that's just the slideythings sitting at their default position if you want to blend from their moving state smoothly into an idle state.

First grab the AnimationStates from the animation then do something like this (pseudo-ish code):


IEnumerator StartupRoutine()
{
heatAnimState.enabled = true;
heatAnimState.normalizedSpeed = 1;
while(heatAnimState.normalizedTime < 1)
{
yield return null; //wait until animation is done. its a coroutine so it won't freeze the game
}
heatAnimState.enabled = false;
workingAnimState.enabled = true;
workingAnimState.wrapMode = loop;
workingAnimState.normalizedSpeed = 1;
}

IEnumerator ShutdownRoutine()
{
//start the heat anim at the end and play it backwards
heatAnimState.enabled = true;
heatAnimState.normalizedTime =1;
heatAnimState.normalizedSpeed = -1;

workingAnimState.weight = 1;
idleAnimState.weight = 0;
idleAnimState.enabled = true;
while(idleAnimState.weight < 1 || workingAnimState.weight > 0)
{
idleAnimState.weight = mathf.movetowards(idleAnimState.weight, 1, delta); //fading the weight of idle to one
workingAnimState.weight = mathf.movetowards(workingAnimState.weight, 0, delta); //fading the weight of the working anim to zero
yield return null;
}

idleAnimState.enabled = false;
workingAnimState.enabled = false; //these animations are done, turn them off

while(heatAnimState.normalizedTime > 0)
{
yield return null; //now wait for the reverse heating anim to finish
}
heatAnimState.enabled = false;
//done!
}

You should make sure the heating animation doesn't have any keyframes for the positions of the other objects so it doesn't interfere.

Then, for your turn on/turn off methods, start the respective coroutines


void TurnOn()
{
StartCoroutine(StartupRoutine());
}

Hope this helps..

Edit: when you're initializing everything and getting the animationState from the animation, I think you have to do animation.blend(animationName) or something.

Here's a snip of me setting up firing and reloading animations from my game:


if(GetComponent<Animation>())
{

//animationSetup
fireAnim = GetComponent<Animation>()["fireAnimation"];
fireAnimSpeed = (fireRateRPM*fireAnim.length)/60;
fireAnim.normalizedSpeed = 0;
fireAnim.enabled = true;
fireAnim.wrapMode = WrapMode.ClampForever;
GetComponent<Animation>().Blend("fireAnimation");

if(!infiniteMag)
{
reloadAnim = GetComponent<Animation>()["reloadAnimation"];
reloadAnimSpeed = reloadAnim.length/reloadTime;
reloadAnim.normalizedTime = 1;
reloadAnim.enabled = true;
reloadAnim.wrapMode = WrapMode.ClampForever;
GetComponent<Animation>().Blend ("reloadAnimation");
}
}

Edited by BahamutoD
changed && to || in the weight fading 'while loop' condition
Link to comment
Share on other sites

  • 2 weeks later...

Hey BahamutoD, thank you very much for your help. I managed to make these complex animations available and it looks very promising for the first shot.

Here is what I did for start (no pseudo-code)

heatAnimation.enabled = true;
heatAnimation.normalizedSpeed = 0.5f;
while (heatAnimation.normalizedTime < 1)
{
yield return null;
}
heatAnimation.enabled = false;


workAnimation.enabled = true;
workAnimation.wrapMode = WrapMode.Loop;
workAnimation.normalizedSpeed = 0.5f;

and for end

heatAnimation.enabled = true;
heatAnimation.normalizedTime = 1;
heatAnimation.normalizedSpeed = -0.5f;


workAnimation.enabled = true;
workAnimation.wrapMode = WrapMode.Loop;
workAnimation.normalizedSpeed = 0.5f;


while (workAnimation.normalizedTime < 1)
{
yield return null;
}
workAnimation.enabled = false;

while (heatAnimation.normalizedTime > 0)
{
yield return null;
}
heatAnimation.enabled = false;

The only struggle I have currently is the setup for the animations. I already received bug reports with NullReferenceExceptions inside those two methods and I expect, that this is happening, because the animation states where not initialized but where null.

Here is the code i have. I currently call it inside the OnLoad method. Is that the correct way or should I call it from OnStart?

foreach (var animator in part.FindModelAnimators("workshop_emissive"))
{
heatAnimation = animator["workshop_emissive"];
heatAnimation.speed = 0;
heatAnimation.enabled = true;
heatAnimation.wrapMode = WrapMode.ClampForever;
animator.Blend("workshop_emissive");
break;
}
foreach (var animator in part.FindModelAnimators("work"))
{
workAnimation = animator["work"];
workAnimation.speed = 0;
workAnimation.enabled = true;
workAnimation.wrapMode = WrapMode.ClampForever;
animator.Blend("work");
}

I cannot reproduce the exceptions and the reporter has a very heavily modded install. Can you tell me if this setup is right or do you have any Idea what could cause those exceptions based on the info you have?

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...