1.通过键盘键值移动
//按下WSAD键使游戏对象前后左右移动
void Update () {
float h = Time.deltaTime *3f; //移动速度
if (Input.GetKey(KeyCode.A))
{
transform.Translate(new Vector3(-h, 0, 0));}
if (Input.GetKey(KeyCode.D)){
transform.Translate(new Vector3(h, 0, 0));}
if (Input.GetKey(KeyCode.W)){
transform.Translate(new Vector3(0, 0, h));}
if (Input.GetKey(KeyCode.S)){
transform.Translate(new Vector3(0, 0, -h));
}
}
2.通过刚体移动
//Horizontal水平轴 (x轴)
//Vertical//垂直轴(z轴)
Rigidboay r;
public float speed;//速度
void Start()
{ <