Unity - 如何让我的跳跃动画循环工作?

Abb*_*bby 4 c# animation unity-game-engine

我是 unity 和 C# 的新手,所以希望得到任何帮助。

我已经让我的精灵跳跃工作正常,但是,唯一可以播放的动画是着陆动画。起飞动画不会播放,精灵在跳跃时保持空闲位置,直到速度低于 0,然后播放着陆动画。

我究竟做错了什么?我希望在玩家跳起来时实现起飞动画播放,然后在它坠落时直接进入着陆动画。

这是我的代码:

using UnityEngine;

public class Player : MonoBehaviour
{
    private Rigidbody2D myRigidbody;

    private Animator myAnimator;

    [SerializeField]
    private float movementSpeed;

    private bool facingRight;

    private bool attack;

    private bool slide;

    [SerializeField]
    private Transform[] groundPoints;

    [SerializeField]
    private float groundRadius;

    [SerializeField]
    private LayerMask whatIsGround;

    private bool isGrounded;

    private bool jump;

    private bool airControl;

    [SerializeField]
    private float jumpForce;

    // Use this for initialization
    void Start()
    {
        facingRight = true;
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
    }

    void Update()
    {
        HandleInput();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");

        HandleMovement(horizontal);

        isGrounded = IsGrounded();

        Flip(horizontal);

        HandleAttacks();

        HandleLayers();

        ResetValues();
    }

    private void HandleMovement(float horizontal)
    {
        if (myRigidbody.velocity.y < 0)
        {
            myAnimator.SetBool("land", true);
        }

        if (!myAnimator.GetBool("slide") && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack")&&(isGrounded || airControl))
        {
            myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);

        }
        if (isGrounded && jump)
        {
            isGrounded = false;
            myRigidbody.AddForce(new Vector2(0, jumpForce));
            myAnimator.SetTrigger("jump");

        }
        if (slide && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
        {
            myAnimator.SetBool("slide", true);
        }
        else if (!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Slide"))
        {
            myAnimator.SetBool("slide", false);
        }
        myAnimator.SetFloat("speed", Mathf.Abs(horizontal));
    }

    private void HandleAttacks()
    {
        if (attack && !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
        {
            myAnimator.SetTrigger("attack");
            myRigidbody.velocity = Vector2.zero;
        }

    }

    private void HandleInput()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            jump = true;
        }
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            attack = true;
        }

        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            slide = true;
        }
    }

    private void Flip(float horizontal)
    {
        if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
        {
            facingRight = !facingRight;

            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;
        }
    }

    private void ResetValues()
    {
        attack = false;
        slide = false;
        jump = false;
    }

    private bool IsGrounded()
    {
        if (myRigidbody.velocity.y <= 0)
        {
            foreach (Transform point in groundPoints)
            {
                Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);

                for (int i = 0; i < colliders.Length; i++)
                {
                    if (colliders[i].gameObject != gameObject)
                    {
                        myAnimator.ResetTrigger("jump");
                        myAnimator.SetBool("land", false);
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private void HandleLayers()
    {
        if (!isGrounded)
        {
            myAnimator.SetLayerWeight(1, 1);
        }
        else 
        {
            myAnimator.SetLayerWeight(1, 0);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Gin*_*der 5

我认为您设置动画的方式使这比必要的更具挑战性。让我们改变一些东西,希望能让你的角色动画更容易一些。

首先,我认为trigger在编写跳跃动画脚本时使用动画是不可靠的。一个更好的方法是float在你的动画师中创建一个,我称之为velocityY,它代表玩家的Rigidbody2D.velocity.y. 我还创建了一个新的bool名为isGrounded,因为我觉得这是更清晰,更适用于多“跳”的场景。

创建这些变量后,您可以通过以下方式链接您的三个动画 - 空闲、跳跃和着陆:

  1. 将默认动画设置为“空闲”。
  2. 从“空闲”过渡到“跳跃”,条件是:

    • velocityY > 0
    • isGrounded = false
  3. 在以下条件下从“跳跃”过渡到“着陆”:

    • velocityY < 0
    • isGrounded = false
  4. 当 时从“土地”过渡到“空闲” isGrounded = true。

  5. 最后,要在角色跌倒时设置动画(无需先跳跃),您可以选择从“空闲”过渡到“着陆”,其中:

    • velocityY < 0
    • isGrounded = false

现在来看代码。这是我在一个项目中测试的一个工作示例,以实现您想要的结果。请注意,我没有在您的脚本中包含所有内容,仅包含让角色正确移动并为其跳跃设置动画的部分。尝试使用此脚本并尝试使用移动值以及播放器Rigidbody2D组件上的重力乘数;默认值和 3.5 的重力乘数对我来说很有趣!

using UnityEngine;

public class Player : MonoBehaviour
{
    //Components on Player GameObject
    private Rigidbody2D myRigidbody;
    private Animator myAnimator;

    //Movement variables
    [SerializeField]
    private float movementSpeed = 9; //Set default values so you don't always
    [SerializeField]                //have to remember to set them in the inspector
    private float jumpForce = 15;

    //Ground checking
    [SerializeField]
    private Transform groundPoint;
    [SerializeField]
    private float groundRadius = 0.1f;
    [SerializeField]
    private LayerMask whatIsGround;

    private float velocityX;
    private bool isGrounded;
    private bool facingRight;

    // Use this for initialization
    private void Start()
    {
        facingRight = true;
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
    }

    private void Update()
    {
        Flip();
        HandleInput();
        HandleAnimations();
    }

    private void FixedUpdate()
    {                       
        HandleMovement();  //It's generally considered good practice to 
                           //call physics-related methods in FixedUpdate
    }

    private void HandleAnimations()
    {
        if (!isGrounded)
        {
            myAnimator.SetBool("isGrounded", false);

            //Set the animator velocity equal to 1 * the vertical direction in which the player is moving 
            myAnimator.SetFloat("velocityY", 1 * Mathf.Sign(myRigidbody.velocity.y));
        }

        if (isGrounded)
        {
            myAnimator.SetBool("isGrounded", true);
            myAnimator.SetFloat("velocityY", 0);
        }
    }

    private void HandleMovement()
    {
        isGrounded = Physics2D.OverlapCircle(groundPoint.position, groundRadius, whatIsGround);

        velocityX = Input.GetAxis("Horizontal");

        myRigidbody.velocity = new Vector2(velocityX * movementSpeed , myRigidbody.velocity.y);
    }

    private void HandleInput()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Jump();
        }
    }

    private void Jump()
    {
        if (isGrounded)
        {   //ForceMode2D.Impulse is useful if Jump() is called using GetKeyDown
            myRigidbody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        }

        else 
        {
            return;
        }       
    }

    private void Flip()
    {
        if (velocityX > 0 && !facingRight || velocityX < 0 && facingRight)
        {
            facingRight = !facingRight;

            Vector3 theScale = transform.localScale;
            theScale.x *= -1;

            transform.localScale = theScale;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我还花了一些时间来重新组织您的代码。你现在不一定需要关心太多的组织,但我认为你可能会感兴趣,因为你还在学习。

如您所见,脚本中的每个方法都处理一个具体的任务。例如,有一种方法专门用于处理动画,另一种方法可以让玩家跳跃。以这种方式设置您的代码是个好主意,这样如果您以后必须更改一个方面,例如玩家移动,那么所有相关代码都在同一个位置。我认为这对于创建处理玩家“动作”(如跳跃或攻击)的方法尤其如此;如果你有足够的它们,你甚至可以创建一个完整的 Actions class。

最后要提到的是这行代码:

isGrounded = Physics2D.OverlapCircle(groundPoint.position, groundRadius, whatIsGround);
Run Code Online (Sandbox Code Playgroud)

我发现这是一种更简单的方法来确定玩家何时停飞。我通过向GameObject播放器添加一个孩子,添加一个便于放置的彩色图​​标来实现这一点(您可以通过单击 a 名称附近的彩色框来实现此目的GameObject),并将其放置在玩家的脚之间。