方向键控制移动
方法1:使用transform.Translate()函数
using UnityEngine;
public class MoveDemo : MonoBehaviour
{
private void Update()
{
GetInput();
}
//获取键盘输入的操作
public void GetInput()
{
if (Input.GetKey(KeyCode.W))
{
this.gameObject.transform.Translate(Vector3.forward * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
this.gameObject.transform.Translate(Vector3.down * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
this.gameObject.transform.Translate(Vector3.left * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
this.gameObject.transform.Translate(Vector3.right * Time.deltaTime);
}
}
}
方法2:使用Rigidbody2D组件
using UnityEngine;
public class MoveDemo : MonoBehaviour
{
public float moveSpeed = 3.0f;
Rigidbody2D rb2d;
Vector2 inpu