RTS Camera Rotation/Movement

Hello Everyone!
I am making a game which has a RTS camera view, basically a top-down camera view. I have made the camera move, scroll and rotate but when it rotates, it doesn’t really feel the movement. If i rotate the camera 180 degrees, the movement will be reversed. How can stop the reverse movement when rotating the camera

Camera Movement Script:

public float panSpeed = 20f;
public float panBorderThickness = 10f;
public Vector2 panLimit;

public float scrollSpeed = 20f;
public float minY = 20f;
public float maxY = 120f;

  void Update() {

Vector3 pos = transform.position;

if (Input.GetKey(KeyCode.W))
      {
        pos.z +=panSpeed * Time.deltaTime;
        }
         if (Input.GetKey(KeyCode.S))
           {
            pos.z -=panSpeed * Time.deltaTime;
            }
           if (Input.GetKey(KeyCode.D))
      {
        pos.x +=panSpeed * Time.deltaTime;
        }
         if (Input.GetKey(KeyCode.A))
           {
            pos.x -=panSpeed * Time.deltaTime;
            }
 
           float scroll = Input.GetAxis("Mouse ScrollWheel");
            pos.y -= scroll * scrollSpeed * 100f * Time.deltaTime;
      
           pos.x = Mathf.Clamp(pos.x, -panLimit.x, panLimit.x);
           pos.y = Mathf.Clamp(pos.y, minY, maxY);
           pos.z = Mathf.Clamp(pos.z, -panLimit.y, panLimit.y);

           transform.position = pos;
     }
  }

Camera Rotation Script:

public float speed = 3.5f;
     private float X;
     private float Y;
     void Update() {
         if(Input.GetMouseButton(0)) {
             transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * speed, -Input.GetAxis("Mouse X") * speed, 0));
             X = transform.rotation.eulerAngles.x;
             Y = transform.rotation.eulerAngles.y;
             transform.rotation = Quaternion.Euler(X, Y, 0);
         }
     }
}

Please fix your indentation; it makes your code difficult to read.

Your problem description was a little unclear, but I think what is happening is that your input is based on global directions (e.g. pressing W always moves “north” in your world) but you want it to be based on directions relative to the camera’s perspective (e.g. pressing W always moves the camera towards the top of your screen).

There’s a few possible ways to address that. What I’d probably do is calculate the current frame’s movement as a separate vector (rather than immediately adding it to “pos”), and then rotate that vector based on the camera’s current orientation before you add it to “pos”.

Rotating the vector might be as simple as multiplying by transform.rotation (in general, quaternion * vector = rotated vector), but I suspect you’ll need to compensate for the fact that the camera is also angled “down” (depends on the details of how your object hierarchy is built). You might do that by changing which button corresponds to which axis, or by making the camera be a child of some other game object that handles the rotations that you want to take into account for the input controls (only).