Camera movement

Hi,the code below will move the camera on the z and x axis only if the rotation of the camera is 0,however
my camera is at a angle( x 30) and when moved along the z axis the camera also moves along the y axis because of the angle.
Could you tell me how to move the camera along the z axis only with the camera at a angle as the angle is needed for my game but not the y movement when moved along the z axis.

public class DragCamera : MonoBehaviour {

    public float speed = 10.0f;
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    Vector3 dir = new Vector3();   //create 0,0,0
   
    if(Input.GetKey(KeyCode.W))dir.z += 1.0f; 
    if(Input.GetKey(KeyCode.W))dir.y += 0;  //test restrict y movement does not work
   
    if(Input.GetKey(KeyCode.S))dir.z -= 1.0f; 
    if(Input.GetKey(KeyCode.S))dir.y -= 0; //test restrict y movement does not work
   
    if(Input.GetKey(KeyCode.A))dir.x -= 1.0f; 
    if(Input.GetKey(KeyCode.D))dir.x += 1.0f; 
   
    dir.Normalize();
   
    transform.Translate(dir * speed * Time.deltaTime);
   
    }//end update

Currently you’re moving the camera relative to itself, so its Z-axis is wherever the camera is pointing. You want to move it relative to the world, where the Z-axis is always the same. Change the last line to this:

transform.Translate(dir *speed*Time.deltaTime, Space.World);

Thats works perfect, thx v.much,
also I would like to limit the movement to just the map so which method would be best
to limit the movement on the x and z axis?

Basic Camera movement complete,
and again thx v.much for the help :slight_smile:

public float speed = 10.0f;
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    Vector3 dir = new Vector3();   //create 0,0,0
   
    if(transform.position.z < 33f){
    if(Input.GetKey(KeyCode.W))dir.z += 1.0f; 
    }
    if(transform.position.z > -5f){
    if(Input.GetKey(KeyCode.S))dir.z -= 1.0f; 
    }
    if(transform.position.x > 0f){
    if(Input.GetKey(KeyCode.A))dir.x -= 1.0f; 
    }
    if(transform.position.x < 50f){
    if(Input.GetKey(KeyCode.D))dir.x += 1.0f; 
    }
   
    dir.Normalize();
   
    transform.Translate(dir * speed * Time.deltaTime, Space.World);
   
    }//end update
   

}