0

我是 Unity 的新手,我刚开始学习动画和动画师。我正在制作一个简单的游戏,玩家可以走路(当时正在播放走路动画)或击球(当时正在播放攻击动画)也可以跳跃(播放跳跃动画)。否则播放器处于空闲状态(空闲动画播放)。问题是,当我尝试击中时,它没有击中(可以看到动画师开始播放击中动画,但随后开始空闲动画)。我的观点是,渲染一帧后,调用另一个更新,因此即使攻击尚未结束,也会播放空闲动画。我的代码如下所示:

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

public class KnightController : MonoBehaviour {

    private Rigidbody2D _rigidBody;
    private Animator _animator;
    private bool _isGrounded;
    private bool _isJumped;
    private bool _isAttacking;

    void Start () {
        _rigidBody = GetComponent<Rigidbody2D>();
        _animator = GetComponent<Animator>();
        _isGrounded = true;
        _isJumped = false;
        _isAttacking = false;
    }

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

        if (Mathf.Abs(walkSpeed) > float.Epsilon && _isGrounded)
        {
            Vector2 oldVelocity = _rigidBody.velocity;
            _rigidBody.velocity = new Vector2(walkSpeed * 3, oldVelocity.y);
            transform.localScale = new Vector2(Mathf.Sign(walkSpeed), transform.localScale.y);
            _animator.Play("KnightWalk");
        }

        if (Input.GetKey(KeyCode.W))
        {
            _isJumped = true;
        }

        if (Mathf.Abs(walkSpeed) <= float.Epsilon && _isGrounded && !_isAttacking)
        {
           // _animator.Play("KnightIdle");
        }

        if (Input.GetMouseButtonDown(0))
        {
            _animator.Play("KnighAttack");
        }
    }
    private void FixedUpdate()
    {
        if (_isJumped)
        {
            if (_isGrounded)
            {
                _rigidBody.AddForce(new Vector2(0, 160));
                _animator.Play("KnightJump");
            }
            _isJumped = false;
        }
    }
    private void OnCollisionStay2D(Collision2D collision)
    {
        _isGrounded = true;
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        _isGrounded = false;
    }
}

关于如何修复它的任何建议?如果我做错了什么,请不要犹豫,请告诉我。

4

1 回答 1

1

是的,你做错了。您的假设是正确的,您强制使用 Animator.Play() 方法播放动画。您应该使用动画控制器的状态、条件和转换。看看这个:https ://docs.unity3d.com/Manual/class-AnimatorController.html

于 2017-10-01T20:18:55.960 回答