- Home /
Press button to play "Aim" animation without repeating.
Hi everyone. I have a problem. I made a script for my character to play an "aiming" animation when I press "LeftShift" , and when I let go, it goes back to "Idle" animation, which is perfectly great. The problem is, when I press "LeftShift" button to aim, the animation keeps looping.. I want it to stop looping but stays steady till I release the "leftShift button. Is it possible?
Here is my script.
Also I have another problem. I have another animation for shooting. I can't really find a way to connect it with the script. Like when I click the"LeftShitf" button and "F" button, it shoots. Can someone please edit my script or give me an idea what should I do ?
NOTE: I'm using the animator so all my animations are in the animator.
Answer by Mr. Mud · Feb 02, 2015 at 12:13 PM
The solution to the looping animation is by turning off the toggle "Loop Time" in the animation which starts aiming. This way, once it reaches the end of the animation, the object will remain in whatever pose it was at that time; until another animation is started of course. You could also opt to create another animation which's keeps aiming, where the transition is "Exit Time" with a value of 0.9 or something like that; that way you can make the object seem more lively, as it still moves.
For your issue with shooting, add another parameter to your AnimationController (my advice would be a trigger). Only when your character is aiming, you can enter that animation. Here is an example of how your animator could look. The horizontal arrows use the transition "Exit Time". You could (depending on the animation) assign the same animation to "Release Aim" as you would to "Take Aim", but with a negative playback Speed.
//Get a reference once; more efficient.
private Animator anim = GetComponent<Animtor>();
[SerializeField]//Assignable from inspector.
private KeyCode //Make the controls more dynamic.
aimKey,
fireKey;
private void Update()
{
if (Input.GetKeyDown(aimKey))
{
anim.SetBool("WeaponRaise", true);
}
if (Input.GetKeyUp(aimKey))
{
anim.SetBool("WeaponRaise", false);
}
if (Input.GetKeyDown(fireKey))
{
anim.SetTrigger("Shoot");
//Do anything else related to shooting.
}
}
Also why are you using an image of script, rather than copy-pasting it? I hope this helps you solve your trouble.
I think I almost got it, but there is an error I don't know how to fix it. it says "(9,25): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponent(System.Type)'"
In this line :
//Get a reference once; more efficient. private Animator anim = GetComponent();
$$anonymous$$ight be one of two things: I didn't test this code, so I skipped that issue. It seems that you cannot acces instance methods to set default values. Quick fix: move it to "Awake()" or "Start()". Another slight issue, it must read Animator; not "Animtor" (without the second 'a').
Or you did not include "Animator" surroundend arrows.
Also note that this is a C# script, so it might need to be rewritten a bit should you use javascript (or boo script for that matter). Something I cannot quite figure out from your sample.
Here is a revised part of the script.
//Get a reference once; more efficient.
private Animator anim = null;
private void Awake()
{
anim = GetComponent<Animator>();
}
Answer by SnotE101 · Feb 03, 2015 at 05:08 AM
Try:
bool aim = Input.GetKey(KeyCode.LeftShift);
anim.SetBool("WeaponRaise",aim);