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!