Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by firepro20 · Jan 16, 2020 at 02:18 PM · camera-movementpanzoom-in

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);
         }
 
 
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image firepro20 · Jan 16, 2020 at 02:18 AM 0
Share

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);
             }
         }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges