How to stop animation when it gets to the end.

There are plenty of answers to this question, but non seem to help my case.
This is what I have done so far and why I need to get the length, and maybe there is even a different way of doing what I’m trying to do.

I have two animations that run simultaneously and they can also be ran backwards.

I have a plane, and the plane has a cockpit and landing gears.

The landing can be down, and the cockpit animation can be ran while the landing gear animation is on as well.

I was able to do this by creating each animation with a different layer. The layers are set to be additive.
I added two float parameters and added each one to me a multiplier.

110169-anim.png

I created a script that attaches to the game object to start the animations.

public class AnimController : MonoBehaviour {

   public Animator aries;

void Start () {
        aries = GetComponent<Animator>();
    }

void Update () {
        if (Input.GetKeyDown("1"))
        {
            aries.SetFloat("CockpitDirection", 1);
            aries.Play("Cockpit");
        }
        if (Input.GetKeyDown("2"))
        {
            aries.SetFloat("CockpitDirection", -1);
            aries.Play("Cockpit");
        }
    }
}

When I hit “1”, the cockpit animation starts and opens the cockpit, and when I hit “2” the animation reverses and starts to close.

The problem I found is that the timer for the animation keeps running after it has ended, so if I let one minute go by after the cockpit is open, and then I hit “2” to close the cockpit, the animation will will take one minute for it to start closing.

My thoughts were if i stop the animation, or pause the animation when the animation is over, When I press “2” to close the cockpit, it will not take X amount of time for the cockpit to close.

Maybe I’m thinking of this wrong, and maybe there is another way of doing this?

If you guys could help, that would be amazing. Thank you for taking the time to read this long post and trying to figure this out with me.

I guess you should double click on the animation and uncheck Loop

Apparently animation.play() has a few extra parameters you can pass:
The first is the animationClip name, then you can pass the layer or -1 if you dont want it, and the third one is fixedTime, or normalized time. This is a number from 0 to 1 where 0 is the beginning, and 1 is the end.

This was my ending script for my three animations. Please note that the direction parameters were set in the editor as -1 as default values.

110264-params.png

my animController script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimController : MonoBehaviour {

    private Animator aries;
    private AnimatorStateInfo cockpitState, landingGearState, thrusterState;
    private bool cockpitOn, landingGearOn, thrusterOn;

    void Start () {
        aries = GetComponent<Animator>();
    }

    void Update () {
        cockpitState = aries.GetCurrentAnimatorStateInfo(0);
        landingGearState = aries.GetCurrentAnimatorStateInfo(1);
        thrusterState = aries.GetCurrentAnimatorStateInfo(2);
        if (Input.GetKeyDown("1"))
        {
            cockpitOn = PlayAnimation("Cockpit", cockpitOn, cockpitState.normalizedTime);
        }

        if (Input.GetKeyDown("2"))
        {
            landingGearOn = PlayAnimation("LandingGear", landingGearOn, landingGearState.normalizedTime);
        }
    
        if (Input.GetKeyDown("3"))
        {
            thrusterOn = PlayAnimation("Thrusters", thrusterOn, thrusterState.normalizedTime);
        }

    }

    private bool PlayAnimation(string type, bool typeOn, float normalizedTime)
    {
        float playTime = normalizedTime % 1;
        if (typeOn && normalizedTime > 1)
        {
            playTime = 1.0f;
        }else if (!typeOn && (normalizedTime > 1 || normalizedTime < 0))
        {
            playTime = 0.0f;
        }    
        if (aries.GetFloat(type + "Direction") >= 1)
        {
            typeOn = false;
            aries.SetFloat(type + "Direction",-1);
            aries.Play(type ,-1, playTime);
        }
        else
        {
            typeOn = true;
            aries.SetFloat(type + "Direction", 1);
            aries.Play(type ,-1, playTime);
        }

        return typeOn;
    }
}