如何在自上而下的游戏中添加备用空闲方向



我正在制作一款自上而下的游戏,我的玩家精灵和动画已经完成。当没有输入时,精灵应该进入空闲状态,但面向相同的方向。我有不同的运动和空转动画集,我不知道如何在空转动画中编码以面对不同的方向。精灵仍然设置动画,但在释放输入后不会移动。

我不知道我是否解释正确,所以作为一个例子,我会说球员向左走。左箭头键被释放,因此没有更多的输入,并且精灵停止移动。但是,动画仍在继续。我想停止此动画并输入另一个动画,该动画仅在没有输入的情况下使用,但同时面向与上次输入相同的方向。这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
movement = movement.normalized;
if (movement != Vector2.zero)
{
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}

谢谢你的帮助!

在该代码中,您实际上并没有启动任何类型的空闲动画,也许这就是为什么?试着在if(movement!=vector2.zero((之后放一个else语句,并让它将所有浮点值设置为0,这样它就不再重复该动画了。

相关内容

最新更新