camera movement in world space, but when the rotated on y axis

I am trying to make a camera that moves exactly like the scene view camera. The camera in the scene view only moves in the x-z plane no matter what the rotation is. meaning, if you change the rotation it doesn’t go right through the planar landscape in a game. But with current code. When the point the point the camera into the landscape and I press forward it would go right through it. would like it move only x and z axis.

   function Start () {


var speed = 5.0f;
var movement =;

function Update () {

   movement.z = Input.GetAxis("Vertical");
   movement.x = Input.GetAxis("Horizontal");
   transform.Translate(movement * speed * Time.deltaTime, Space.self);                           


One solution would be to store the transform’s y position, and reset it to that value after you’ve done your translation:

var originalHeight=transform.position.y;
transform.Translate(movement * speed * Time.deltaTime, Space.self);

Note that your forward/backward movement will become very slow if you are looking way up/down. To compensate for this, scale your movement.z by the factor determined by that angle:

// place your two movement assignments here
var planarForward=transform.forward;
movement.z /= planarForward.magnitude;

EDIT: Note the shift in height is exactly movement.z*transform.forward.y, so instead of the originalHeight method you could just use: