public class Move1 : MonoBehaviour
{
Rigidbody2D rb;
Vector2 inputPos;
public float moveSpeed;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();//获取刚体部件
inputPos = new Vector2();
}
// Update is called once per frame
void Update()
{
//获取键盘操作的水平数值
inputPos.x = Input.GetAxisRaw("Horizontal");
//获取键盘操作的垂直数值
inputPos.y = Input.GetAxisRaw("Vertical");
//移动速率
rb.velocity = inputPos * moveSpeed;
}
}
Unity中使用键盘控制2D物体移动
最新推荐文章于 2024-05-18 11:17:39 发布