using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f; // 移动速度
private Rigidbody2D rb;
void Start()
{
// 获取 Rigidbody2D 组件
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// 获取水平和垂直输入(使用键盘的 WASD 或方向键)
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// 计算移动速度
Vector2 moveVelocity = new Vector2(horizontalInput, verticalInput) * moveSpeed;
// 应用移动速度到 Rigidbody2D
rb.velocity = moveVelocity;
}
}