Animation stops as soon as it starts.

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"); 
}

are you calling animation.Play() anywhere else?

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.

I tested what you said about another animation being called so i made a new project with nothing there and it still does the same thing.

Bump

Please someone help

Does the swim animation cause the player to exit the collider, thus stopping the animation

No the player only plays the animation when he first touches the collider and just for a split second

Make it as triggerstay instead of triggerenter.

Thank you! On trigger stay worked but now there is the problem where the animation is bugging out it looks likes it vibrating how can i fix this?

bool animationbool = false;

function update()
{
if(animationbool)
controller.animation.Play(“swim”);
}
function OnTriggerEnter (col : Collider)

{

var controller : ThirdPersonController = col.GetComponent(ThirdPersonController);

if (controller != null)

print (“enter”);
animationbool = true;

}

function OnTriggerExit (col : Collider)

{

var controller : ThirdPersonController= col.GetComponent(ThirdPersonController);

if (controller.IsGrounded !=null)

controller.animation.Stop(“swim”);
animationbool = false;
print(“We are grounded”);

}

Check out this…!

function OnTriggerStay (col : Collider)

{

var controller : ThirdPersonController = col.GetComponent(ThirdPersonController);

if (controller != null)

print (“enter”);
if(controller.animation.Isplaying == false)
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”);

}

Check this first…!

What do you mean?

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.

Where in the script would i add the boolean?

bool hasAnimationPlayed = false;

void OnTriggerStay(Collider col)
{
    if(!hasAnimationPlayed)
    {
        animation.Play();
        hasAnimationPlayed = true;
    }
}

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.

I still dont understand how to add that code to the script everywhere i type it i end up with 7 errors im sorry…

Forgot to mention it’s in C#, sorry. And I did say to change it to suit your needs =/
Here, I’ve converted it for you and included your animation.

var hasAnimationPlayed : boolean = false;

function OnTriggerStay(col : Collider)
{
    if(!hasAnimationPlayed)
    {
        var controller : ThirdPersonController = col.GetComponent(ThirdPersonController);
        if (controller != null)
            controller.animation.Play("swim");
        hasAnimationPlayed = true;
    }
}

Now it is back to just playing the animations when i first touch the collider.

Have you tried removing your earlier codes and just using the one I gave you?

EDIT
Sorry, I misread. You need to turn the boolean to false if you want to replay the animation. I’d recommend putting it in OnTriggerExit.

if(controller.animation.Isplaying == false)
controller.animation.Play(“swim”);

Add this line where ur playing