Animation Looping Help

Here is my scrpt

function Update () {
if(Input.GetKeyDown("mouse 0")) {
    animation.Play("m4shoot");
}
if(Input.GetKeyUp("mouse 0")) {
    animation.Stop("m4shoot");
}
}

When i let go of mouse 0 the animation stops even if its not done. Can you tell me so when you let go of mouse 0 the animation finishes instead of just stops.

Shooting animations shouldn’t be looping. You should handle that in your shooting logic. You would fire the animation everytime you shoot a bullet.

The point of looping animations is that they are never done so you need the animation to be a “normal” once-animation.

var rateOfFire = 5.0;
private var shootDelay = 0.0;
private var shootTimeOut = 0.0;

function Start()
{
    var shootAni = animation["m4shoot"];
    shootDelay = (1.0/rateOfFire);
    // This will adjust the speed of the animation ot match the rate of fire
    shootAni.speed =  shootAni.length / shootDelay;
    shootAni.wrapMode = WrapMode.Once;
}

function Update ()
{
    if(Input.GetKey("mouse 0"))
    {
        if (Time.time > shootTimeOut)
        {
            shootTimeOut = Time.time + shootDelay;
            animation.Play("m4shoot");
            // shoot your bullet here
        }
    }
}

The code isn’t tested but it should work that way :wink:

Well, if you have a neutral animation, and a shoot animation, you would use animation.CrossFade(), if not, use animation.Pause.

If you want the animation “m4shoot” to play fully, remove the key up handler that stops it!

function Update () {
   if(Input.GetKeyDown("mouse 0")) {
       animation.Play("m4shoot");
   }
}