Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why is my Walking Animation Taking so Long to Play?

Discussion in 'Animation' started by FGPArthurVII, Jul 15, 2016.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    Hello I'm making a 2D game, when nothing moves the player keeps idle on the Dan_Standing Amim, when I press RIght Arrow it walks passing to the Dan_Walking anim, but the point is when I play the game, the movement is all right, but to change the animation I have to press the button for to long, It's not as imediate as the movement itself, I've already check the animations and they are fine, and in the Animator I noticed that the bool changes its value imediately, however it takes too long to make the transition.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     public Rigidbody2D rBody;
    7.     public Vector2 speed;
    8.     private Animator anim;
    9.  
    10.     void Start ()
    11.     {
    12.         rBody = GetComponent<Rigidbody2D>();
    13.         anim = GetComponent<Animator>();
    14.     }
    15.    
    16.     void FixedUpdate ()
    17.     {
    18.         if (Input.GetKey(KeyCode.RightArrow))
    19.         {
    20.             anim.SetBool("isWalking", true);
    21.             rBody.MovePosition(rBody.position + speed * Time.fixedDeltaTime);
    22.         }
    23.         else
    24.         {
    25.             anim.SetBool("isWalking", false);
    26.         }
    27.     }
    28. }
    29.  
    Thanks;
    Arthur.
     
  2. SethMeshko

    SethMeshko

    Joined:
    Sep 20, 2014
    Posts:
    109
    You have a public Vector2 controlling for speed so I would assume that you are setting that value in the editor, or possibly leaving it at zero. You are also controlling your rigidbody with a function that is its position plus its speed multiplied by each fixed frame. I'm not exactly sure why you are doing that. But what you are basically telling it is that it should increase it's speed every frame at what would be an exponential rate and that number controls its position. I believe that the delay is happening because your character is building up speed to finally start moving, but the value of speed is probably so small to begin with that it causes that delay. If you were to continue to press the right arrow key you should keep accelerating indefinitely. Re-evaluate that function, if you want it to move immediately you won't want it to be multiplied every frame by speed. You would want the force applied to the rigid body to equal some constant number. I'm actually not sure why you are controlling the position of your character with the rigid body either. I would be controlling the position of the object with, well, the position of the object. The rigid body is (as far as I know) used for physics calculations not for controlled movement.
     
  3. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    Solved It by unchecking a box on Inspector for the transitions, but about the movePosition, I couldn't use transform.position for the movement because this method is not the most adequate for RigidBody physics based objects, so I used RigidBody2D.Move Position, Which in the parentesis goes the current position + or - the new position (or speed, since speed is just another position on space of the value you gave to it) multiplyed by Time.deltaTime, but as I'm using FixedUpdate, Time.fixedDeltaTime.