如何让动画等待Unity中的另一个动画
how to make animation wait for another animation in Unity
我是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;
}
}
关于如何修复它有什么建议吗?如果我做错了什么,请不要犹豫告诉我。
是的,你做错了。您的假设是正确的,您强制使用 Animator.Play() 方法播放动画。你应该使用动画控制器的状态、条件和转换。看看这个:https://docs.unity3d.com/Manual/class-AnimatorController.html
我是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;
}
}
关于如何修复它有什么建议吗?如果我做错了什么,请不要犹豫告诉我。
是的,你做错了。您的假设是正确的,您强制使用 Animator.Play() 方法播放动画。你应该使用动画控制器的状态、条件和转换。看看这个:https://docs.unity3d.com/Manual/class-AnimatorController.html