How do I make an animation play once forward while holding down a button/key and then play the same animation backwards when I let go of that button?

In this case the animation I want to have played is aiming for my FPS. I’ve got the aiming down pat but when I let go of the key it just stays there aiming still. This is what my code looks like right now…

fuction Update() {

 If(input.getbuttondown("fire2")) {
      Getcomponent.<Animation>().Play("aim"); }

Then under it I have another script for aim and shoot…

 If(input.getkeydown("fire2"));
      If(input.getkeydown("fire1")) {
           Getcomponent.<Animation>().Play("aimandshoot"); } }

The script works I just can’t get it to revert back to it’s original state. Please help

You might want to look at using Animation Controllers which exist to help you manage the states of your animation, including how and when you can move from one animation to another.

In this case, you can set triggers / bools in the animation controller to specify if you’re aiming and how you can transition into that animation (if you’re allowed to - you’ll find later on that when yo have more states like ‘reloading’, using a state machine takes away the effort of having to check if you can move from one state to the other manually).

Normally you’d have one animation for ‘idle to aim’ and another for ‘aim to idle’. However, if you really want to share the same animation clip then you can just set the animation speed to -1 (i.e. reverse). Note I’ve not actually tried this so I may be wrong.

Have you tried using GetButtonDown for “Fire1” and GetButton for “Fire2”? That way, you should be able to click the mouse0 button to shoot, and hold the mouse1 button to aim. So, something like:

function Update () {
	if (Input.GetButtonDown("Fire1")) {
		GetComponent.<Animation>().Play("aim");
		}
	if (Input.GetButton("Fire2")) {
		if (Input.GetButtonDown("Fire1")) { 
			GetComponent.<Animation>().Play("aimandshoot");
		}
	}
}