你可以使用 Unity 的 Rigidbody2D 组件来实现俯视角人物的四向移动。
首先,你需要在人物的游戏对象上添加 Rigidbody2D 组件。然后,可以通过以下代码实现四向移动的功能:
using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
public float speed = 5f; // 移动速度
private Rigidbody2D rb; // 声明一个 Rigidbody2D 变量
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// 获取水平方向输入(左右移动)
float horizontalInput = Input.GetAxis("Horizontal");
// 获取垂直方向输入(上下移动)
float verticalInput = Input.GetAxis("Vertical");
// 计算移动向量
Vector2 movement = new Vector2(horizontalInput, verticalInput) * speed;
// 移动人物
rb.velocity = movement;
}
}
在这段代码中,我们通过获取水平方向和垂直方向的输入,并使用 Rigidbody2D 的 velocity 属性来控制人物的移动。
你可以在 Unity 的 Edit > Project Settings > Input 中设置控制人物移动的按键。
希望这些信息能帮到你!