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 /
  • Help Room /
avatar image
0
Question by RTKeulen · May 23, 2017 at 07:09 PM · rotationrotatearoundrotate-around

How to limit rotation from RotateAround?

I'm setting up a camera system to let the user pan the camera with an SHIFT + RMB + drag movement, as well as rotate the view around a point in space with RMB + drag.

So far, i've done this with an empty "CameraHolder" gameobject, with the mainCamera as child. The CameraHolder rotates about its y-axis to orbit left/right, and translates to pan the camera. The mainCamera uses transform.RotateAbout to orbit around the CameraHolder in the up/down direction.

However, i'm having trouble locking the up/down direction to 90 degrees up/down from the neutral plane. The camera either just continues in a circle around the CameraHolder, or rotates around the pole, both resulting in the controls for up/down inverting. I'd like to limit the movement to going from directly above, looking down upon the point to directly below, looking up to the point. So far, this is my code:

 //Re-align camera towards cameraholder.
 Camera.main.transform.LookAt(CameraHolder);
 
 //RMB + drag
 //orbit up/dn/l/r
 if (Input.GetMouseButton(1) && !Input.GetKey(KeyCode.LeftShift)){
 CameraHolder.Rotate(new Vector3(0,1,0) * Input.GetAxis("Mouse X")); //orbit the view l/r by rotating CameraHolder
 Camera.main.transform.RotateAround(CameraHolder.position, Vector3.left, Input.GetAxis("Mouse Y"));    //orbit the view up/dn by rotating camera around CameraHolder
 }
 
 //SHIFT + RMB + drag
 //translate up/dn/l/r
 if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.LeftShift)){
 CameraHolder.Translate( new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0)); //translate the view by translating CameraHolder
 }

I've tried using Mathf.Clamp, but didn't manage to figure out how to apply that to a RotateAround. Any help with limiting the up/down orbit would be greatly appreciated.

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 RTKeulen · May 23, 2017 at 07:11 PM 0
Share

(This is called every void Update() btw.)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by neosca · May 24, 2017 at 12:05 PM

You can do something like this to clamp rotation in a particular axis:

float sensitivity = 4f;

Camera.main.transform.LookAt(CameraHolder);

if (Input.GetMouseButton(1) && !Input.GetKey(KeyCode.LeftShift)){

float newRotationX =Camera.main.transform.localEulerAngles.x - Input.GetAxis ("Mouse Y") * sensitivity;

newRotationX = Mathf.Clamp (newRotationX, 35f, 75f);

float newRotationY = Input.GetAxis ("Mouse X") * sensitivity;

Camera.main.transform.RotateAround(CameraHolder.position, Vector3.up, -newRotationY );

float rotateX = newRotationX - Camera.main.transform.localEulerAngles.x;

Camera.main.transform.RotateAround(CameraHolder.position, Camera.main.transform.left, rotateX );

}

Comment
Add comment · Show 1 · 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 RTKeulen · May 25, 2017 at 11:25 PM 0
Share

Thanks for your answer. While this does work for most values, it sadly doesn't work for 90 and 0 degrees, or for going below the horizontal plane.

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

121 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

Related Questions

Rotating Camera around Player's X-axis while rotating Player around its Y-axis (using mouse input) 0 Answers

Rotate an object around one of his childs in the self space (not the world space) 2 Answers

AngleAxis and MoveRotation not rotating around the object 0 Answers

Axis are not rotated as I would expect. 0 Answers

Clamp camera orbit rotation using RotateAround 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