- Home /
How to avoid animation from resetting after its completion?
I have a basic scene where I have a barrier like the ones at the tollbooth stations. Barrier has an animation where it goes from 0 degrees to 90 degrees in order to open and close the barrier. Now, I control the barrier such that pressing s plays the animation and w reverses the animation. Here is the Script:-
public Animation cylinderAnim;
void Update()
{
if(Input.GetKeyDown(KeyCode.S))
{
cylinderAnim.GetComponent<Animation>()["CylinderMove"].speed=2;
cylinderAnim.GetComponent<Animation>().Play("CylinderMove");
}
else if(Input.GetKeyDown(KeyCode.W))
{
cylinderAnim.GetComponent<Animation>()["CylinderMove"].speed=-1;
cylinderAnim.GetComponent<Animation>().Play("CylinderMove");
}
else if((Input.GetKeyUp(KeyCode.S)) || (Input.GetKeyUp(KeyCode.W)))
{
cylinderAnim.GetComponent<Animation>()["CylinderMove"].speed=0;
cylinderAnim.GetComponent<Animation>().Play("CylinderMove");
}
But the problem is that it resets the animation after completion i.e. it jumps straight to 0 degree angle from 90 degree angle. I just want it to pause right there and not move anymore than 90 degrees. Now I know the culprit here is 'wrapmode.once' as it resets the animation. Loop and pingpong will also obviously not work. ClampForever comes very close to what I want to do but it does what its name suggests, it literally clamps forever i.e. if i keep the button s pressed, the animation will pause, but the clamp value just keeps on increasing, and thus if i press w to play animation in reverse, it will first decrease the clamp value and then starts playing the animation. I tried setting up the ontriggerstay/ontriggerenter at the other end so that if the barrier's end enters this trigger, it should stop like in this script:-
void OnTriggerStay(Collider other) //or ontriggerenter
{
other.GetComponent<Animation>()["CylinderMove"].speed=0;
other.GetComponent<Animation>().Play("CylinderMove");
}
This works, but if i keep on tapping the 's' it will finally go through my trigger and then will either reset or clampforever depending on the wrapmode. Also, I cannot just place a collider to stop the barrier, because when the barrier hits the collider, it just goes off the rails and messes everything up. So, What do have to do to just pause the animation or restricting it between o and 90 degrees only. Also, can i set the min and max values for wrapmode.clampforever because that will also solve my problem. Thanks!
Answer by Mehrdad995 · Jul 12, 2015 at 01:38 PM
I almost don't have any experience on animations so it might be much easier than my way.
if i were you i would try to use coroutine and yield just like when we want to play an audioclip and stop it after it played.
this script should work. let me know if anything got wrong.
using UnityEngine;
using System.Collections;
public class AnimationPlayer : MonoBehaviour
{
public Animation cylinderAnim;
public AnimationState ast;
bool animPlaying;
void Start()
{
ast = cylinderAnim.GetComponent<Animation>()["CylinderMove"];
StartCoroutine("PlayAnimOnButton");
}
IEnumerator PlayAnimOnButton()
{
while(true)
{
if(Input.GetKeyDown(KeyCode.S) && !animPlaying)
{
yield return StartCoroutine(AnimPlay("CylinderMove",2.0f));
}
else if(Input.GetKeyDown(KeyCode.W) && !animPlaying)
{
yield return StartCoroutine(AnimPlay("CylinderMove",-1.0f));
}
if((Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.W)) && animPlaying)
{
ast.speed = 1.0f;
cylinderAnim.Stop(); // if you want to stop only a special anim, write its name between parentheses;
animPlaying = false;
}
}
}
IEnumerator AnimPlay(string animClipName, float speed)
{
animPlaying = true;
cylinderAnim.Play(animClipName);
ast.speed = speed;
yield return new WaitForSeconds(ast.length);
cylinderAnim.Stop();
animPlaying = false;
yield return null; // optional as long as we have the yield above
}
}
Your solution didn't work, thanks for the help anyways. I solved it myself using vector3.angle and checked whether the angle was less than 1 degrees.:-)
Your answer
![](https://koobas.hobune.stream/wayback/20220611230830im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animation not playing completely 1 Answer
Multiple Cars not working 1 Answer
Programmatically created AnimationClip will not loop 2 Answers
Clamping an animation does not work. Help! 0 Answers
Distribute terrain in zones 3 Answers