- Home /
How to use mecanim to play specific animation clips when public static int condition is reached?
Im currently using a public static int condition to play various animations, for e.g.
using UnityEngine;
using System.Collections;
public class CubeRiseControl : MonoBehaviour
{
public AnimationClip CubeRise;
void Update ()
{
if (SetButInc.nextNum == 2)
{
animation.Play("CubeRise");
}
}
}
To call animation clips in this way they need to be in legacy, type 1. However, the downside of switching to legacy animation clips is that they limit how I can animate the RGBA values, more specifically how to fade some objects but not others. If I go back to using type 2 animation clips, can I use mecanim/animator to do the same job, i.e. play an animation clip only when a public static int condition is met?
Not looking for a full solition, just a nudeg in the right direction. Thanks.
Answer by Baste · Oct 10, 2014 at 03:35 PM
This is exactly what mechanim is made for.
You set up your mechanim with a transition to the clip CubeRise on a trigger, and set that trigger when you need to switch the animation.
With that setup, your code would be:
public Animator CubeAnimator;
void Update ()
{
if (SetButInc.nextNum == 2)
{
CubeAnimator.SetTrigger("CubeRise");
}
}
Now, there's something strange going on - you're starting the animation clip on every frame. Are you supposed to be doing that? If you're going to be using mechanim, you want to send the trigger message only once for every time you switch to that clip, so you'd have to rewrite your system to take that into account.
That's just what I wanted to hear. RE: Update(), for reasons unknown to me this is the only way I could get my Type 1 animations play, they were set to Clamp Forever so they would only play once then stop on the last frame. I agree this isn't very logical and I questioned it when it was suggested to me.
I have my animation clips set to Type 2 and have added your script however I'm not sure how to set the trigger as you suggest. I see the trigger parameter window in mecanim but can't understand how to set it. At present my 3 animation clips play in loop as as soon as I start the game.
Ok, I think I understand the trigger parameter now. Lets say I have an Idle animation where nothing happens and CubeRise where the cube rises by 20 in Y. I want the game to start with the cube Idle and only animate CubeRise when nextNum = 2, as in above script.
In my animator controller I have added the two clips with a transition going from Idle to CubeRise with a condition = CubeRise (trigger parameter). Im now expecting that when I start the Idle animation plays in loop until nextNum = 2, where the trigger CubeRise condition is met and CubeRise animation plays. However the result I have is that CubeRise animation plays as soon as the game starts. I think im still missing something somewhere along the line?
Answer by neeeb · Oct 13, 2014 at 10:21 AM
Ok, I now start on IdleCubeStart with the CubeRise Trigger condistion to CubeRise animation. Then exit time = 1 transition to IdleCubeRisen (cube idle in risen position). Still the same problem CubeRise animation playing from the start, when it should be triggered by script when nextNum=2. All other objects like text react perfectly to nextNum values so I know that is not the issue.
So it turns out I was being a fool and left the trigger checked ON when it should have been OFF. Thanks for your help!