public class move : MonoBehaviour {
public float speed;
//private Rigidbody body;
void Start()
{
speed = 40f;
//body = this.GetComponent<Rigidbody>();
}
void Update () {
//第一种直接修改Transform属性
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);
}
//第二种移动方式:给刚体施加力,如果是球体,会发生旋转
//if(Input.GetKey(KeyCode.W))
//{
//body.AddForce(Vector3.forward*speed,ForceMode.Force);
//}
//if(Input.GetKey(KeyCode.S))
//{
//body.AddForce(Vector3.back*speed,ForceMode.Force);
//}
//if(Input.GetKey(KeyCode.A))
//{
//body.AddForce(Vector3.left*speed,ForceMode.Force);
//}
//if(Input.GetKey(KeyCode.D))
//{
//body.AddForce(Vector3.right*speed,ForceMode.Force);
//}
// 第三种移动方式:直接获取游戏的水平以及垂直值,可以直接在Update方法中声明
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
this.transform.Translate(new Vector3(h*Time.deltaTime*speed,0f,v*Time.deltaTime*speed));
}
}