Home > front end >  Unity animation not completing
Unity animation not completing

Time:02-07

i'm beginner on unity and game development. i'm trying to make a platformer game with unity, and i want to play a animation when the player is moving right or left, but the problem is that the isMoving variable for some reason is true animation is playing for a few moments (less than a second) and then is going to false again and the idle animation is playing.

My Animator (Exit time is disabled for all transitions):

My Animator

My Movement script:

public class PlayerMovement : MonoBehaviour
{

    private bool canPlayerJump;
    private float horizontalInput;
    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        

        if (Input.GetKeyDown(KeyCode.Space) && canPlayerJump)
        {
            GetComponent<Rigidbody>().AddForce(Vector3.up * 5, ForceMode.VelocityChange);
        }

        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.A))
        {
            animator.SetBool("isMoving", true);
        } else
        {
            animator.SetBool("isMoving", false);
        }

        horizontalInput = Input.GetAxis("Horizontal");

    }

    private void FixedUpdate()
    {
        GetComponent<Rigidbody>().velocity = new Vector3(horizontalInput * 5, GetComponent<Rigidbody>().velocity.y, 0);
    }

    private void OnCollisionEnter(Collision collision)
    {
       canPlayerJump = true;
       Debug.Log(collision.gameObject.name);
    }

    private void OnCollisionExit(Collision collision)
    {
        canPlayerJump = false;
    }

}

CodePudding user response:

You using Update loop function that means this code will be executed each frame by unity. Similar to FixedUpdate, LateUpdate etc... You can read more here: HERE Once you pressing down you IF statement will be executed because you listening for BTN DOWN event. This means that TRUE will be returned only in one frame when you press actually button next frame it will return false. and your animations stops.

  1. Try to change bool to Trigger
  2. Or handle in another way bool var state.

CodePudding user response:

Input.GetKeyDown

is only true in the one frame the button went down.

Returns true during the frame the user starts pressing down the key identified by the key KeyCode enum parameter.


You rather want to use Input.GetKey which stay true as long as the key stays pressed.

Returns true while the user holds down the key identified by the key KeyCode enum parameter


alternatively in order to not have redundant accesses you could also simply use

horizontalInput = Input.GetAxis("Horizontal");

animator.SetBool("isMoving", !Mathf.Approximately(horizontalInput, 0));

And as a general note: Cache the component references and don't use GetComponent over and over again!

[SerializeField] private Rigidbody _rigidbody;

private void Awake ()
{
    if(!_rigodbody) _rigidbody = GetComponent<Rigidbody>();
}

then later everywhere use _rigidbdoy instead of GetComponent<Rigidbody>()

  •  Tags:  
  • Related