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
Answer by phil_me_up · Mar 02, 2016 at 01:29 PM
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.
http://docs.unity3d.com/Manual/class-AnimatorController.html
Answer by mine00123 · Mar 22, 2016 at 02:05 PM
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");
}
}
}
Your answer
Follow this Question
Related Questions
Reload animation makes gun too high 0 Answers
Flipping Animation/Jumping animation 0 Answers
Animator Position VS Script Vector 3 0 Answers
Meeting Animation parameters with an AI sprite. 0 Answers
Interchangeable animations in a script? 0 Answers