Hi im new the unity I have a swimming script that uses colliders and everytime my character touches the collider the swim animation starts and the stops instantly and that is the same for some other animations that dont even involve colliders how can i fix this?
Here is the swimming script.
function OnTriggerEnter (col : Collider)
{
var controller : ThirdPersonController = col.GetComponent(ThirdPersonController);
if (controller != null)
print ("enter");
controller.animation.Play("swim");
}
function OnTriggerExit (col : Collider)
{
var controller : ThirdPersonController= col.GetComponent(ThirdPersonController);
if (controller.IsGrounded !=null)
controller.animation.Stop("swim");
print("We are grounded");
}
the default being StopSameLayer… sounds a lot like you start the swim animation from the trigger function, but somewhere else another animation is being called and the swim is getting stopped.
The reason why your animation is bugging out is because OnTriggerStay is called on every frame as long as the collider is touching the trigger. So as long as OnTriggerStay is called, your animation will reset since it’s forced to play every time OnTriggerStay is called. You’ll probably want to set a boolean so it only triggers the animation to play once.
Then in your exit, just set the boolean false so when you reenter the collider, it’ll play the animation. And don’t copy and paste, please change what is necessary.