3
\$\begingroup\$

i clicked a cube and added a simple transform animation by using animation timeline, and the cube has a "animator" component was added into the inspector. My questions are

  1. how can i set the animation do not play automatically? I can't find any related option in animator in inspector

  2. how can i set the script to control the animation? for example, play the animation after the scene started 5 seconds?

\$\endgroup\$
  • \$\begingroup\$ You will need to create an empty animation in the animation window and set that as your default state. Then create a parameter that you will set to initialize the transition. Make a transition from your default state to your desired state with the parameter as the condition. Call that parameter through a script after obtaining the reference to the Animator. Alter the parameter in the script by using animator.SetBool,SetInteger,SetFloat etc etc... \$\endgroup\$ – Savlon Jul 8 '15 at 8:07
5
\$\begingroup\$

The animator view in Unity is a little confusing, so I've added screenshots for each step.

Step 1: Set Up States

The animator system has two default states, Entry and Any State. Entry is the point that the animator starts at when the scene starts. Right click on the background and create new Empty State. I called mine "NoAnimation," but you can call it whatever makes sense to you.

Right click on this and say "Set As Layer Default State". You'll see that the arrow from the entry state goes to this new Empty state now.

Your state that has the animation now needs a way to be entered. Right click on the Empty state and select "Make Transition." Drag the arrow to your existing state. Your screen should look like this:

State Design

By default, the transition you just created will execute at the end of the animation in the no animation state. Since this takes no time, it will just automatically move. To remedy this, you need to create a trigger.

Step 2: Create A Trigger

In the parameters panel, click the add button and select Trigger.

Create Trigger

I named my trigger "AndGo" but you should choose a name that makes sense to you.

Named Trigger

Step 3: Hook Trigger to Transition

Now, to make the transition happen when the trigger is triggered, select the NoAnimation state and then click the transition to your animation state in the inspector view. This will open up more options about that transition.

Uncheck "Has Exit Time" and then click the plus sign under the Conditions Box. The name of your trigger should show up as a Condition.

Trigger set to Transition

Step 4: Add a behavior to set the trigger

Finally, you need to create a Behaviour to attach to the object that will wait 5 seconds and then trigger the transition. I achieved this with a coroutine.

using UnityEngine;
using System.Collections;

public class DelayStart : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(delayThenStart());
    }

    private IEnumerator delayThenStart()
    {
        yield return new WaitForSeconds(5f);

        // get a reference to the animator on this gameObject
        var animator = gameObject.GetComponent<Animator>();

        animator.SetTrigger("AndGo");
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.