上下左右 4个方向 ,同时按上和左右 可以搭配使用
private void FixedUpdate() {
Vector3 vector3 = new Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0) * Time.deltaTime * 5;
transform.Translate( vector3,Space.World);
}
使用具体按键控制 ,这种相对来说说灵活一些 ,比如我要屏蔽w上的方向控制
private void FixedUpdate() {
// float axis = Input.GetAxis("Vertical");
// axis = axis > 0 ? 0 : axis;
// Vector3 vector3 = new Vector3(Input.GetAxis("Horizontal"),axis,0) * Time.deltaTime * 5;
// transform.Translate( vector3,Space.World);
int x = 0;
int y = 0;
if (Input.GetKey (KeyCode.A)) {
x+= -1;
}
if (Input.GetKey (KeyCode.D)) {
x+= 1;
}
if (Input.GetKey (KeyCode.S)) {
y+= -1;
}
if (Input.GetKey (KeyCode.W)) {
y+= 1;
}
transform.Translate( new Vector3(x,y,0) * Time.deltaTime * 5,Space.World);
}
GetKey判断当前某个键是否按着 ,按着某个键就可以一直走
GetKeyDown 是按下的某一帧 ,不持续的,走不起来的