# Camera movement using WASD on x/z plane only

I’ve been wearing my enter key down on google searches - I have a camera script based on the MouseOrbit.js asset. That’s all working fine, but in addition to the basic orbiting and the zooming that I’ve added, I would like to use the WASD keys to move the camera around the world.

The W key would move the camera straight forward, however it would ignore the y axis. For example, using

``````transform.Translate(Vector3.Forward*Time.Delta*20);
``````

moves the camera forward relative to the camera. This results in you quickly hitting the ground. Moving back oibviously does the opposite. The desired effect is sliding across the world without getting any closer/farther to it, regardless of the angle the camera is at.

The closest I can get is using the Space.World parameter of Translate(), but this does not take into account the rotation of my camera. I think if I could take that into account, this would be solved but I’m not clear how to do that.

Mahalo for any assistance.

Chris

You don’t need to use transform.Translate. Just calculate how much the camera should move forward for a given frame (something like if the W key is held down do deltaPos = transform.forward * Time.deltaTime * 20), set the Y value of that Vector3 to zero, then add that delta vector to the original position by adding it to the current position transform.position += deltaPos;

I was faced with the same problem. This is my solution that worked for me.

``````// x axis
Vector3 translation = new Vector3(xValue * Time.deltaTime, 0.0f, 0.0f);
this.transform.Translate(translation, Space.Self);
// z axis
float angle = this.transform.eulerAngles.y * Mathf.Deg2Rad;
translation.x = Mathf.Sin(angle) * (zValue * Time.deltaTime);
translation.z = Mathf.Cos(angle) * (zValue * Time.deltaTime);
this.transform.position += translation;
``````