- Home /
Mecanim animation, playing once
I really hate the mecanim animation system, i can't simply do animation.play("blabla");
i need to do a lot of things just to play an animation lol.
so, i'm here to ask someone how i can play an animation just once, i press a button, and it just play ONCE the animation. i've tryed that:
public Animator weaponAnim;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
StartCoroutine("Shoot");
}
}
public IEnumerator Shoot () {
weaponAnim.SetBool("inShoot", true);
yield return new WaitForSeconds (weaponAnim["Shoot"].lenght);
weaponAnim.SetBool("inShoot", false);
}
and another thing i need is: how can i get an animation lenght? i used weaponAnim["blabla"].lenght
, but it gives me errors.
Answer by SinisterRainbow · Jan 09, 2014 at 08:56 PM
Main Question: In the Project folder, find the animation, and make sure it's NOT set to looping. Once you remember a few basic things to check for the animation system, it's cake. I made a game with custom animations in a 48 hour game jam recently, would never use anything but Unity to attempt this.
"Another thing": animation["ani-name"].length is the correct way to do it. Post a new question with the error messages so someone else can solve it...
Both might be solved by setting up the animation properties properly in the Project folder, i suspect that's where ure main problem lies.
public Animator weaponAnim;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
StartCoroutine("Shoot");
}
}
public IEnumerator Shoot () {
weaponAnim.SetBool("isShoot", true);
weaponAnim.SetBool("isIdle", false);
yield return null;
weaponAnim.SetBool("isShoot", false);
weaponAnim.SetBool("isIdle", true);
}
i have this, but now the shoot animation just play 50% of it
you're only delaying 1 frame before setting shoot to false - that's either the fastest animation ever, or you need to delay longer. :) This is also not the best way to approach animation. You probably want to think about how animations interact with one another. for 2-3 animations something simple is adequate, but on larger systems you likely will want some to crossfade, some animations should wait to finish first, etc..
If you are using Unity 4.3, you can use Animator.SetTrigger("ParameterName") ins$$anonymous$$d of setting true and false.
Unity Reference:
"Triggers are parameters that act mostly like booleans, but get resets to inactive when they are used in a transition."