0
\$\begingroup\$

I have a prefab and I want to control when an animation is initiated. I can't seem to figure out how to do that. I have already created the animation clip, the animator component for the prefab, and the animator controller. The animation works, when I enter play mode in the unity editor (it plays automatically), but I want to control when the animation occurs when providing input (such as pressing on the space bar or clicking on the prefab).

How would I go about doing that?

\$\endgroup\$
1
  • \$\begingroup\$ You'd either use an Animation component's Play() method (disabling playOnAwake), or an Animator component's SetTrigger() method with a transition to your animation keyed to that trigger. \$\endgroup\$ – DMGregory Feb 16 '17 at 5:59
1
\$\begingroup\$

I'd advise you to refer to Unity Documentation for these kind of questions.

What you want to do can be achieved by using Animation States and Transitions.

Basically, you need to create an empty state in the Prefab's Animator Controller and set it to Default (Orange color). And create a transition from this empty state to the state which contains your light animation then set the condition for transition (Which could be bool indicating an user input).

\$\endgroup\$
0
\$\begingroup\$

to call on an animation at will, the object must start in an empty state. To initiate an animation get the animator component for the object and call Play("stateName") on it. Remember to revert back to the empty so that the playable state can be replayed later

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