- Home /
Trigger animation going crazy
When player stays on trigger, it only plays "moveOut" and it loops/repeats crazy, the loop is takin off the animation. So no idea whats going on.
void OnTriggerStay(Collider other)
{
animation.Play("moveOut");
}
void OnTriggerExit(Collider other)
{
animation.Play("moveIn");
}
}
What do you expect, you call animation.Play every physics tick, about 40 times per second, giving it no rest. Of course it will play it constantly
do you have any ideas to help? I know whats happening, just no idea how to fix it, new to scripting in c#
Answer by mustafatufan · Mar 10, 2015 at 09:48 AM
That is normal. If you want to animate just once, try "OnTriggerEnter" instead of "OnTriggerStay".
Answer by HarshadK · Mar 10, 2015 at 06:51 AM
I guess what you are looking for is:
void OnTriggerStay(Collider other)
{
if(!animation.IsPlaying("moveout"))
animation.Play("moveOut");
}
So we only play the moveout animation if it is not already playing while inside the trigger so that the play is not triggered every FixedUpdate.
I tried what you said, but moveOut still keeps repeating itself over and over and moveIn doesnt play at all. No change hmmm
void OnTriggerStay(Collider other)
{
if(!animation.IsPlaying("moveOut"))
animation.Play("moveOut");
}
void OnTriggerExit(Collider other)
{
animation.Play("moveIn");
}
Looks like your moveIn animation is actually making character get back in trigger and then the moveout animation plays and this out and in keeps repeating.
Try:
void OnTriggerStay(Collider other)
{
// If no other animation is playing then only we play moveout animation
if(!animation.isPlaying)
animation.Play("moveOut");
}
void OnTriggerExit(Collider other)
{
// We stop any other animation playing and then play the moveIn animation
animation.Stop();
animation.Play("moveIn");
}
haah well , the moveIn now works now which is great. But moveOut is still repeating itself : /
So do you want to run moveout only once when the player is in the trigger?
If so, then call the moveOut inside OnTriggerEnter rather than OnTriggerStay.
Is your moveOut animation set to loop. Check for this animation import settings in Inspector.
Answer by DiNoGames · Mar 10, 2015 at 10:06 AM
If you only want to play it once, then you shouldn't call it in OnTriggerStay which occurs every frame the object is within the trigger.
Use OnTriggerEnter instead which occurs only once.
Yeah I tried that, it makes sesne, butttt it still repeats itself, and it makes lil sense to why at this point : /
Your answer
![](https://koobas.hobune.stream/wayback/20220613185644im_/https://answers.unity.com/themes/thub/images/avi.jpg)