How to avoid camera going upwards when looking up

I have made a simple scene and added this simple script to camera (it’s for looking around and walking forward for now, I don’t like using pre-made code):

#pragma strict


var mouse_x : float;
var mouse_y : float;

var sensitivity_x : float = 1.5;
var sensitivity_y : float = 1.5;
function Start () {

}

function Update () {
mouse_x = Input.GetAxis ("Mouse X");
mouse_y = Input.GetAxis ("Mouse Y");

transform.Rotate(Vector3.up * mouse_x * sensitivity_x, Space.World);
transform.Rotate(Vector3.left * mouse_y * sensitivity_y, Space.World);
transform.eulerAngles.z=0;

if (Input.GetKey (KeyCode.W))
{
	transform.Translate(Vector3.forward * Time.deltaTime, Space.Self);
}
}

The problem is, when I look in the sky and press “W” (walk forward), the camera also moves in the sky.
How do I avoid it and keep camera at the same level? I could probably set Z coordinate to some constant value, but then how would I move around a terrain?

Try this:

if (Input.GetKey (KeyCode.W)) {
    var v3 = transform.forward;
    v3.y = 0.0;
    transform.Translate(v3.normalized * Time.deltaTime, Space.World);
}