- Home /
How to play an Animation only once from my MachineState Animator?
Hi, I created a MachineState Animator with blendtrees, and many animations. Many of these animations has a input (GetButtonDown). For example, I put the animation "Jump" with the input "space". What happens is that when I press a lot of times the "space" button the animations goes to the start point and my character literally flies in the field. What I need is when I press "space" the "Jump" animation must go to the end, even when I press the same button many times.
My code is this:
using UnityEngine;
using System.Collections;
public class TestMotion : MonoBehaviour {
private Animator myAnimator;
// Use this for initialization
void Start () {
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
myAnimator.SetFloat ("VSpeed", Input.GetAxis ("Vertical"));
myAnimator.SetFloat ("HSpeed", Input.GetAxis ("Horizontal"));
if (Input.GetButtonDown ("Jump")) {
myAnimator.SetBool ("Jumping", true);
Invoke ("StopJumping", 0.08f);
}
void StopJumping() {
myAnimator.SetBool("Jumping", false);
}
Answer by coolraiman · Feb 17, 2016 at 01:41 PM
your animator is a wonderfull state machine, use its power
if (Input.GetButtonDown ("Jump") && !(myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Jump")))
{
...
}
also, you may want to use triggers instead of bool when a trigger is activated and then used to fill a condition to start an animation in your animator, it is turned off it allow to use much less hard coding
Thank you coolraiman for your answer. I agree with you, my question its more a question about preventing the player from jumping while being in a jump animation. I tried your suggestion with your if condition, but something is missing me:
if (Input.GetButtonDown ("Jump") && !(myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Jump")))
{
myAnimator.SetBool ("Jumping", true);
Invoke ("StopJumping", 0.08f);
}
I put this code but didn't work. The code has no errors, the animator runs nicely, but the problem persist. Can you help me? I also unchecked the loop time in the inspector.
The (0) is the Layer Index. Change it to whatever layer your Jump anim is on.
I only have the Base Layer, so the "0" is correct? Where are the information about the number of the layers?
Answer by Guhanesh · Feb 17, 2016 at 12:56 PM
Uncheck Loop Time in the inspector of your Animation files.
its more a question about preventing the player from jumping while being in a jump animation
Thx Guhanesh, my animations has the loop time unchecked, but it still has this problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220612064507im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer
Why does my transition freeze the second animation on it's first frame? 1 Answer
Dynamically create states and transition using Animator Controller 1 Answer
Is there a way to change the WrapMode of all Animations in a Mecanim Animator? 1 Answer
Animation clip 'BackgroundAnim' is not retargetable? 0 Answers