Unity 5 Delay in animation

Asked

Viewed 1,190 times

4

I just created animation to run and jump with Unity, but I have a problem when the player goes back to the initial state of Idleplayer, when I stop running or jump it continues for another second, I have tried to decrease the frames but nothing. Follow my code below and a screenshot of the animation setup:

public class Player : MonoBehaviour
{


    public float velociadade;


    public Transform player ;       
    public Transform Ground;  
    public Animator animator;
    public bool isGround;
    public float force = 200f;    
    public float jumpTime = 0.5f;
    public float jumpDelay = 0.5f;      
    public bool jumped ;

    void Start ()
    {           
        animator = player.GetComponent<Animator> ();    
    }   

    void Update ()
    {
        Movimentar ();
    }

    void Movimentar ()
    {
        isGround = Physics2D.Linecast(this.transform.position,Ground.position,1<<LayerMask.NameToLayer("Plataforma"));


        animator.SetFloat ("run", Mathf.Abs (Input.GetAxis ("Horizontal")));


        if (Input.GetAxisRaw ("Horizontal") > 0) {
            transform.Translate (Vector2.right * velociadade * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 0);
        }


        if (Input.GetAxisRaw ("Horizontal") < 0) {
            transform.Translate (Vector2.right * velociadade * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 180);
        }




        bool up = Input.GetKeyUp(KeyCode.Space);
        if (up && isGround && !jumped)
        {               
            GetComponent<Rigidbody2D>().AddForce(transform.up * force);
            jumpTime = jumpDelay;
            animator.SetTrigger("jump");   
            jumped = true;


        }


        jumpTime -= Time.deltaTime;

        if (jumpTime <= 0 && isGround && jumped) {
            animator.SetTrigger("ground");
            jumped = false;
        }   
    }

inserir a descrição da imagem aqui

  • 2

    Hello. From what I can tell, your transition rule of runPlayer for idlePlayer is when the value of run is less than 0.1. A possible problem I see is that the value of Input.GetAxis() is in the range [-1.. 1], and thus will be negative (and consequently less than 0.1! ) if the player moves to the left, triggering this transition erroneously. You may have a similar error in the other transition (from idlePlayer for runPlayer) which leads erroneously to runPlayer?

  • opa exactly, it is giving the idleplayer error to the runPlayer... I tried to change the values there and it didn’t work... so I understood the b.o is time to get the value in Input.Getaxis() correct?

  • Not exactly. The way it is, it won’t work when the character moves to the left, because the value of Input.GetAxis() will be negative (-1), then less than 0.1 and the animation will change from runPlayer for idlePlayer. I assumed that you also have the reverse problem, because by the way you did not understand like the Input.GetAxis() works. Change the transition value from "less than 0.1" to "equal to 0" and see if it works. The idea is that when your character stops moving because the user stopped pressing the arrows on the keyboard, the value returned by GetAxis will be 0.

  • He’s walking both ways, the problem is when I stop pressing the arrows both sideways and up he continues for another second or so running or jumping...

  • 2

    Um... I think you’re the one who didn’t understand what I told you. Reread my comments and reread the documentation of the function Input.GetAxis(). The problem does not seem to be the movement, but the machine of animation states. In other words, it seems to me that your transitional rules are wrong.

  • 1

    After reading the documentation a few times I understood... vlw is now working ...

  • Not at all. Since you solved it, why don’t you create an answer yourself explaining in detail what you did to fix it? I could do it, but I don’t know exactly what you’ve changed, so you can do it without a problem. Having an answer accepted is good because "mark" the question as answered and also helps more easily other people in the future who have similar problems.

  • 1

    Perfect opa, I will go up here ... dai is already recorded as solved.. vlw the tip...

  • 1

    What has been changed to fix this problem, I am in the same doubt. I wait, thank you!

Show 4 more comments

1 answer

2


It’s the transition animation, that other Timeline that gets on the inspector when you select a transition in the Imator. Just click on the white lines with arrows between a state and another, for example, in your screenshot is the line that is blue because it is selected, between 'runPlayer' and 'idlePlayer'. Note that the inspector is displaying a Timeline containing these two animations, it would be for you to choose which part of each would be displayed during the transition.

transição entre as animações

The red circular part is the animation that must occur between the transition from one animation to the other.

You can join the beginning and the end to have no animation and end this second of undue animation for your case.

This video explains more about transition animation animations

  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - From Review

  • well, I think the essential thing is that it is in the right Timeline, in the details of the transition in the Animator tab. But ok, I will explain more here in the answer!

  • @rubStackOverflow is now so good that you’ll even want to learn Unity

Browser other questions tagged

You are not signed in. Login or sign up in order to post.