- Home /
 
Top Down Camera Pan and Zoom
I currently have the following setup for pan and zoom. I am able to maintain the Y component fixed, however, when it comes to Z component, the current code will freeze Z to previous known value unless Y is greater than 120.0f. I would like to be able to still move the camera X and Z component with WASD, while not changing the Z component with scroll wheel once the Y limit is reached.
 private void LateUpdate()
     {
         if (isPlaying) 
         {
             MoveCamera();
         }
     }
 
     private void MoveCamera()
     {
         
         //WASD to move X and Z .. Check camera specs for strategy game, orientation and perspective - orthographic or isometric
         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
         {
             MainCamera.transform.localPosition += transform.forward * cameraMoveSpeed * Time.deltaTime; 
         }
         if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) 
         {
             MainCamera.transform.localPosition += transform.forward * -cameraMoveSpeed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
         {
             MainCamera.transform.localPosition += transform.right * cameraMoveSpeed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
         {
             MainCamera.transform.localPosition += transform.right * -cameraMoveSpeed * Time.deltaTime;
         }
 
         //This little peace of code is written by JelleWho https://github.com/jellewie
         float ScrollWheelChange = Input.GetAxis("Mouse ScrollWheel");           
         if (ScrollWheelChange != 0)
         {                                            
             //If the scrollwheel has changed
             //The radius from current camera
             float R = ScrollWheelChange * 100;                                   
             //Get up and down
             float PosX = MainCamera.transform.eulerAngles.x + 90;              
             //Get left to right
             float PosY = -1 * (MainCamera.transform.eulerAngles.y - 90);       
             //Convert from degrees to radians
             PosX = PosX / 180 * Mathf.PI;                                       
             PosY = PosY / 180 * Mathf.PI;
             //Calculate new coords
             float X = R * Mathf.Sin(PosX) * Mathf.Cos(PosY);                    
             float Z = R * Mathf.Sin(PosX) * Mathf.Sin(PosY);                    
             float Y = R * Mathf.Cos(PosX);
             //Get current camera postition for the offset
             float CamX = MainCamera.transform.position.x;                      
             float CamY = MainCamera.transform.position.y;                      
             float CamZ = MainCamera.transform.position.z;
 
             cameraCurrentPosition = new Vector3(CamX, CamY, CamZ);
             Vector3 finalCameraPosition = new Vector3(CamX + X, CamY + Y, CamZ + Z);
 
             Vector3 newCameraPosition = Vector3.Lerp(cameraCurrentPosition, finalCameraPosition, 10.0f);
             //Move the main camera
             MainCamera.transform.position = newCameraPosition;
             
         }
         // End of little piece of code
 
         // Camera Pan Limits
    
         if (MainCamera.transform.position.y <= 120.0f)
         {
             MainCamera.transform.position = new Vector3(MainCamera.transform.position.x, 120.0f, cameraCurrentPosition.z);
         }
         else if (MainCamera.transform.position.y >= 200.0f)
         {
             MainCamera.transform.position = new Vector3(MainCamera.transform.position.x, 200.0f, MainCamera.transform.position.z);
         }
 
 
     }
 
              I temporarily worked around this by checking for input from W or S and adding a 0.1 to the Y of the camera to allow free movement, it works seamlessly.
 if ($$anonymous$$ainCamera.transform.position.y <= 120.0f)
         {
             $$anonymous$$ainCamera.transform.position = new Vector3($$anonymous$$ainCamera.transform.position.x, 120.0f, cameraCurrentPosition.z);
             if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
             {
                 $$anonymous$$ainCamera.transform.position = new Vector3($$anonymous$$ainCamera.transform.position.x, 120.1f, cameraCurrentPosition.z);
             }
         }
                 Your answer
 
             Follow this Question
Related Questions
How to make a camera zoom in on a specific point 1 Answer
Camera flies far away if lag spike occurs 0 Answers
Pan RTS camera controller without changing elevation/zoom 0 Answers
It is possible to add a Cinemachine ColliderComponent to Cinemachine FreeLookComponent 0 Answers
How to make the camera follow the player while still being able to be rotated? 1 Answer