- Home /
Stop animation from playing
Hey guys, i am having a problem with the Animator component. I have a door, and i want it to open when the player collides with it's trigger, and close when the player isn't in the trigger anymore,like an automatic door, but i am having a problem, the animation is looping. Once i am in the trigger, it go, and then doesn't stop. when it finishes, it starts again, and again, and again. What can i do so the animation is played, and then it stop?
Example: The player is in the door's trigger, play the opening animation. When it is finished, stay there. When the player go out of the trigger, then play the closing animation. When it is finished, stay there until the player is in the trigger again.
Here is my script: using UnityEngine; using System.Collections;
public class Door : MonoBehaviour
{
public Animator anim;
bool trigger;
bool auto;
void Start()
{
auto = false;
}
void OnTriggerStay(Collider other)
{
trigger = true;
auto = true;
}
void OnTriggerExit(Collider other)
{
trigger = false;
}
void Update()
{
if (trigger == true)
{
anim.SetTrigger("Open");
}
if (auto == true)
{
if (trigger == false)
{
anim.SetTrigger("Close");
}
}
}
}
And here is my animator screen:
PLEASE help me. I can't see that animation looping anymore, i am getting dizzy =P
Answer by Runalotski · Dec 02, 2016 at 08:41 PM
I think you should be able to remove the Idle state and then import your animations with 'loop time' un ticked.
Then make a transition from Closed -> Open on if(Open == true) Then make a transition from Open -> Closed on if(Open == false)
Then it should play the animation once and then hold until the state changes
OR
If you want to keep the same set up with the Idle state then make sure you add a bool so it wont go back to idle unless the statement is false so it will follow
In Idle state -> Open == true -> Play Open Door once Then it needs to stay there as to return to idle it will need Open == false
with the animation being set to not loop
None of them seems to work. Removing the "Idle" state make close play, then open play, and it goes. Using bools, sadly i couldn't make it work too, the animations doesn't play. Could you give me an example?