The player is still playing the animation walk, even though I fall into the cliff. I think it's overwriting the fall animation

!!

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private float horizontal;
    private float speed = 12f;
    private float jumpPower = 17.5f;
    private int jumpCount = 0;
    private bool isGrounded = false;

    private SpriteRenderer spriteRenderer;
    private Animator animator;

    [SerializeField] private Rigidbody2D rb;

    private void Start()
    {
        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
    void Update()
    {
        Flip();
        DoubleJump();
    }

    private void FixedUpdate()
    {
        Movement();
    }

    private void Flip()
    {
        if (horizontal < 0f)
        {
            spriteRenderer.flipX = true;
        }
        else if (horizontal > 0f)
        {
            spriteRenderer.flipX = false;
        }
    }

    private void Movement()
    {
        animator.SetFloat("yVelocity", rb.velocity.y);
        animator.SetFloat("xVelocity", Math.Abs(rb.velocity.x));
        // Get Value of -1 and 1
        horizontal = Input.GetAxisRaw("Horizontal");

        //Makes the player move left and right
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }

    private void DoubleJump()
    {
        if (isGrounded)
        {
            jumpCount = 0;
        }

        if (Input.GetButtonDown("Jump") && jumpCount < 2)
        {
            isGrounded = false;
            Jump();
        }
    }

    private void Jump()
    {
        animator.SetBool("isJumping", !isGrounded);
        rb.velocity = new Vector2(rb.velocity.x, jumpPower);
        jumpCount++;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            Debug.Log("Ground Detected");
            isGrounded = true;
            animator.SetBool("isJumping", !isGrounded);
        }
    }
}

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Anything with Animations / Animators / Mechanim:

Only consider the code AFTER you have done this critical step:

Always start with the Animator state machine and prove it works in isolation, no code at all.

Here’s more reading:

1 Like