'Juggled player animation on the slopes in Unity

I have another issue with my animator in 2D. Currently i have only few slopes in game, but it annoys me, because i don't know how to make animation work properly on them. This is one example:

enter image description here

As you can see, there is little slope. Now how my Animator looks like:

enter image description here

The code now looks like it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System;

public class PlayerMovementScript : MonoBehaviour
{
    [SerializeField] float runSpeed;
    [SerializeField] float jumpSpeed;
    [SerializeField] float climbSpeed;

    Vector2 moveInput;
    Rigidbody2D playerRigidbody;
    Animator playerAnimator;
    CapsuleCollider2D playerCapsuleCollider;
    float gravityScaleAtStart;

    void Start()
    {
        playerRigidbody = GetComponent<Rigidbody2D>();
        playerAnimator = GetComponent<Animator>();
        playerCapsuleCollider = GetComponent<CapsuleCollider2D>();
        gravityScaleAtStart = playerRigidbody.gravityScale;

    }
    void Update()
    {
        Run();
        FlipSprite();
        ClimbLadder();
        Falling();
    }

    void OnMove(InputValue value)
    {
        moveInput = value.Get<Vector2>();
        Debug.Log(moveInput);
    }

    void OnJump(InputValue value)
    {
        if (!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }

        if (value.isPressed)
        {
            playerRigidbody.velocity += new Vector2(0f, jumpSpeed);
        }
    }

    void Run()
    {
        Vector2 runVelocity = new Vector2(moveInput.x * runSpeed, playerRigidbody.velocity.y);
        playerRigidbody.velocity = runVelocity;

        bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) > Mathf.Epsilon;

        if (playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            playerAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
        } else
        {
            playerAnimator.SetBool("isRunning", false);
        }
    }

    void FlipSprite()
    {
        bool playerHasHorizontalSpeed = Math.Abs(playerRigidbody.velocity.x) > Mathf.Epsilon;
        if (playerHasHorizontalSpeed)
        {
            transform.localScale = new Vector2(Mathf.Sign(playerRigidbody.velocity.x), 1f);
        }
    }

    void ClimbLadder()
    {
        if (!playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Climb"))) {
            playerRigidbody.gravityScale = gravityScaleAtStart;
            playerAnimator.SetBool("isClimbing", false);
            playerAnimator.SetBool("isClimbingIdle", false);
            return;
        }

        Vector2 climbVelocity = new Vector2(playerRigidbody.velocity.x, moveInput.y * climbSpeed);
        playerRigidbody.velocity = climbVelocity;
        playerRigidbody.gravityScale = 0;

        bool playerHasVerticalSpeed = Math.Abs(playerRigidbody.velocity.y) > Mathf.Epsilon;
        playerAnimator.SetBool("isClimbing", playerHasVerticalSpeed);
        playerAnimator.SetBool("isClimbingIdle", !playerHasVerticalSpeed);
    }

    void Falling()
    {
        if (
            playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Climb"))
            || playerCapsuleCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))
        ) {
            playerAnimator.SetBool("isJumping", false);
            playerAnimator.SetBool("isLanding", false);
            return;
        }

        bool playerHasVerticalSpeed = Math.Abs(playerRigidbody.velocity.y) > Mathf.Epsilon;
        if (playerRigidbody.velocity.y >= 0)
        {
            playerAnimator.SetBool("isJumping", playerHasVerticalSpeed);
            playerAnimator.SetBool("isLanding", false);
        } else
        {
            playerAnimator.SetBool("isJumping", false);
            playerAnimator.SetBool("isLanding", playerHasVerticalSpeed); 
        }   
    }
}

If my player went on slope, Animator shows my animations switch quickly between Run, Jump and Land. How to fix it to save correct jump animation?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source