玩家跳跃动画不在unity2d中播放

Player jumping animation not playing in unity2d

几天来我的跳跃动画一直有问题,我想知道如何做到这一点,当我按下“SPACE”键时,它会播放我的跳跃动画。我的动画师中已经有一个名为 isJumping 的 bool 函数,只是我不知道如何在脚本中调用它来播放我的跳跃动画。我已经尝试了很多教程来找到我的答案,但大多数教程并没有很好地解释如何去做。我已经设置了步行动画,只是我知道需要知道如何制作跳跃动画。

Image of my animator enter image description here

到目前为止我的代码

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

public class PM : MonoBehaviour
{


public float moveSpeed;
public float jumpHeight;
Rigidbody2D rb;
BoxCollider2D boxColliderPlayer;
int layerMaskGround;
float heightTestPlayer;
public Animator animator;

    void Start()
{
    rb = GetComponent<Rigidbody2D>();

    // Get the player's collider so we can calculate the height of the character.
    boxColliderPlayer = GetComponent<BoxCollider2D>();
    // We do the height test from the center of the player, so we should only check
    // halft the height of the player + some extra to ignore rounding off errors.
    heightTestPlayer = boxColliderPlayer.bounds.extents.y + 0.05f;
    // We are only interested to get colliders on the ground layer. If we would
    // like to jump ontop of enemies we should add their layer too (which then of
    // course can't be on the same layer as the player).
    layerMaskGround = LayerMask.GetMask("Ground");
}

    void Update()
    {
    float moveDir = Input.GetAxis("Horizontal") * moveSpeed;
    rb.velocity = new Vector2(moveDir, rb.velocity.y);

    // Your jump code:
    if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() )
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
    }
    animator.SetFloat("Speed",Mathf.Abs(moveDir));
    }


    /// <summary>
    /// Simple check to see if our character is no the ground. 
    /// </summary>
    /// <returns>Returns <c>true</c> if the character is grounded.</returns>
    private bool IsGrounded()
    {
    // Note that we only check for colliders on the Ground layer (we don't want to hit ourself). 
    RaycastHit2D hit = Physics2D.Raycast(boxColliderPlayer.bounds.center, Vector2.down, heightTestPlayer, layerMaskGround);
    bool isGrounded = hit.collider != null;
    // It is soo easy to make misstakes so do a lot of Debug.DrawRay calls when working with colliders...
    Debug.DrawRay(boxColliderPlayer.bounds.center, Vector2.down * heightTestPlayer, isGrounded ? Color.green : Color.red, 0.5f);
    return isGrounded;
 }
} 

不知道你的动画树是怎么设置的。为此,请从您的其他状态进行转换,如果 isJump 设置为 true,那么它将从任何状态转换为跳跃(当然除了跳跃),然后退出您的跳转回到进入或闲置或其他状态跳跃结束时的状态最有意义。如果你 post 你的树的图片,我可以在需要时帮助你。

有了转换和单独的状态设置,并使用布尔值来触发转换,播放动画所需的唯一代码应该是:

animator.SetBool("isJumping", true);

在 isGrounded 函数中检测到玩家再次停飞后,使用相同的代码段但传入 false 以停止跳跃动画。

animator.SetBool("isJumping", false);