Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 YankinSheffield · Jul 20, 2012 at 04:07 PM · mouseorbitcamera controlspanning

Mouse orbit on click and drag/pan problem

I have an object targeted to a main camera, and simply want the user to rotate it on left click and drag, zoom with the scroll wheel, and pan with middle mouse and drag. At the moment, the object rotates every time the mouse is moved, rather than when the LMB is held down. I know it's a simple fix, but can't figure it out.

Also, the pan function just results in my object jittering back and forth. Suggestions for fix?

Thanks!

 var target : Transform;
 var distance = 10.0;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = -20;
 var yMaxLimit = 80;
 
 private var x = 0.0;
 private var y = 0.0;
 
 //From Unity forums for adding middlemouse zoom
 
 var maxDist : float = 200;
 var minDist : float = 30;
 var zoomSpeed : float = 5;
 var panSpeed = 0.3f;
 
 @script AddComponentMenu("Camera-Control/Mouse Orbit")
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
  // Make the rigid body not change rotation
     if (rigidbody)
  rigidbody.freezeRotation = true;
 }
 
 function LateUpdate () {
     if (target) {
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
   
   y = ClampAngle(y, yMinLimit, yMaxLimit);
          
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
         
         transform.rotation = rotation;
         transform.position = position;
     }
     if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance < maxDist){           
            distance += zoomSpeed;                             
            transform.Translate(Vector3.forward * -zoomSpeed); 
     }
     if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance > minDist){     
                distance -= zoomSpeed;                             
                transform.Translate(Vector3.forward * zoomSpeed); 
  }
 
  if (target && Input.GetMouseButtonDown(0)) {
  }
  //panning
  else if (Input.GetMouseButton(2))
         {
             //grab the rotation of the camera so we can move in a psuedo local XY space
             target.rotation = transform.rotation;
             //target.Translate(transform.right * -Input.GetAxis("Mouse X") * panSpeed);
             transform.Translate(transform.right*Time.deltaTime,Space.World);
             target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
         }
  }
 static function ClampAngle (angle : float, min : float, max : float) {
  if (angle < -360)
  angle += 360;
  if (angle > 360)
  angle -= 360;
  return Mathf.Clamp (angle, min, max);
 }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TheCodeMonkey · Jul 20, 2012 at 05:18 PM

if I am not mistaken:

 if (target) {
     x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
     y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

     y = ClampAngle(y, yMinLimit, yMaxLimit);

     var rotation = Quaternion.Euler(y, x, 0);
     var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;

     transform.rotation = rotation;
     transform.position = position;
 }

Is simply rotating based on the mouse itself every frame as long as there is a target you simply have to place a check for mouse input when you check for the target:

 if (target && Input.GetMouseButtonDown(0))
Comment
Add comment · Share
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
0
Wiki

Answer by YankinSheffield · Jul 20, 2012 at 06:18 PM

Thank you for answering, but I'm afraid I don't understand. I'm struggling with coding - I come from a much different background. I thought you meant that I just needed to move the "(target && Input..." line above the x+= Input.GetAxis to check for mouse input.I did that, but now it won't rotate at all, and the zoom in has stopped working. Can you please clarify? Thanks! New code below:

 var target : Transform;
 var distance = 10.0;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = -20;
 var yMaxLimit = 80;
 
 private var x = 0.0;
 private var y = 0.0;
 
 //From Unity forums for adding middlemouse zoom
 
 var maxDist : float = 200;
 var minDist : float = 30;
 var zoomSpeed : float = 5;
 var panSpeed = 0.3f;
 
 @script AddComponentMenu("Camera-Control/Mouse Orbit")
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
  // Make the rigid body not change rotation
     if (rigidbody)
  rigidbody.freezeRotation = true;
 }
 
 function LateUpdate () {
     if (target && Input.GetMouseButtonDown(0)) {
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
   
   y = ClampAngle(y, yMinLimit, yMaxLimit);
          
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
         
         transform.rotation = rotation;
         transform.position = position;
     }
     if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance < maxDist){           
            distance += zoomSpeed;                             
            transform.Translate(Vector3.forward * -zoomSpeed); 
     }
     if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance > minDist){     
                distance -= zoomSpeed;                             
                transform.Translate(Vector3.forward * zoomSpeed); 
  }
 
  
  //panning
  else if (Input.GetMouseButton(2))
         {
             //grab the rotation of the camera so we can move in a psuedo local XY space
             target.rotation = transform.rotation;
             //target.Translate(transform.right * -Input.GetAxis("Mouse X") * panSpeed);
             transform.Translate(transform.right*Time.deltaTime,Space.World);
             target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
         }
  }
 static function ClampAngle (angle : float, min : float, max : float) {
  if (angle < -360)
  angle += 360;
  if (angle > 360)
  angle -= 360;
  return Mathf.Clamp (angle, min, max);
 }
Comment
Add comment · Show 2 · Share
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 TheCodeMonkey · Jul 20, 2012 at 06:44 PM 0
Share

Sorry if I was unclear I did not mean rip it out and throw it up where I was commenting I simply meant to ADD the check in, and leave your other check where it was, your problem was that it was constantly rotating, the way you had the code before it would rotate every time the player moved the mouse no matter what. You needed the mouse check to make sure it worked properly. I will be home shortly and then I will be able to double check.

avatar image TheCodeMonkey · Jul 20, 2012 at 06:50 PM 0
Share

Also the reason it probably is not working currently is because your panning check is an else and is only being checked if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0 && distance > $$anonymous$$Dist) is not true. You needed to leave the check where it was before and not simply move it but add in the mouse input check. so your code would look almost exactly as it did originally with just one added check.

avatar image
0

Answer by YankinSheffield · Jul 23, 2012 at 07:23 PM

Thanks so much for your help, TheCodeMonkey. The zoom and mouse orbit now work as expected. However, even with the "else" removed, the pan is still broken. But now the object just sort of dances off of the screen in a comical fashion. Will keep playing.

Comment
Add comment · Share
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse Orbit snapping issues 0 Answers

Mouse Orbit - Change From Mouse, to Keys 1 Answer

Mouse Orbit Improved (Rotating when Mouse Button is down) 2 Answers

Web player mouse tracking change between 2.6 and 3.0? 0 Answers

Smooth transition of camera targets 2 Answers


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