Gun kickback animation script

im making an fps game with unity and i would like some animation for my gun. ive already made a kickback animation for when i shoot my gun but i dont know how to trigger the animation when i click the left mouse button, can anyone help me?

function Start(){
    animation["kickback"].wrapMode = WrapMode.Once;
}
function Update(){
    //...
    if(Input.GetButtonDown("Fire1"){//or whatever button
        animation.Play("kickback");
        FireWeapon();
    }
}

This requires that you have an Animation component attached to your player object with the correct animation specified therein. "animation" is a courtesy variable supplied by Unity that automatically points to the Animation component attached to the same GameObject. Another point is that you will need to control timing. It's possible that you've already got this taken care of, but you don't want it triggering this every frame that the button is down. There needs to be dead time after each hit. If you've got nothing else, a quick and dirty way would be to use `private var lastFired = 0.0;` and set `lastFired = Time.time;` whenever you shoot, and check `if(Time.time > lastFired + 0.5)` before deciding to fire again. Then again, you might have a better solution already in place for this problem.

//you wrote the script wrong. your missing 1 more ) in the if statement

function Start(){
    animation["kickback"].wrapMode = WrapMode.Once;
}
function Update(){
    //...
    if(Input.GetButtonDown("Fire1")){//or whatever button
        animation.Play("kickback");
        //you can play the animation in the fireWeapon function as well
        FireWeapon();
    }
}

Holy hell i just looked back at this and it was posted 3 years ago lol So noobie of me to miss that :slight_smile:
Thanks guys but I’ve far surpassed what i was doing at the time.

I’m currently working on implementing vehicles using unity into the popular MMO Infestation: Survival Stories. Because there are none from the DEV as of yet. but i think unity’s physics would suit perfectly. Implementing the car is the hard part…