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 krides · Jan 02, 2012 at 03:06 AM · camerarotationupdateeuleranglesfixedupdate

How to restrict rotation properly

How do I restrict rotation angles? The rotation just keeps slipping through the limit and stopping forever. When I set 355 as a limit, it stops at 354.993 or something. Can something be done about it? Is the problem with Update frequency?

I am using this code:

 if(Input.GetAxis("Mouse X")<0)
 {    
     if ((transform.rotation.eulerAngles.y < 4) || (transform.rotation.eulerAngles.y > 355))
     {
         transform.RotateAround(cameraFocus.position, Vector3(0, -1, 0), 5 * Time.deltaTime);
         print(transform.rotation.eulerAngles.y);
     }
 }
 
 //right
 if(Input.GetAxis("Mouse X")>0)
 {
     if ((transform.rotation.eulerAngles.y < 4) || (transform.rotation.eulerAngles.y > 355))
     {
         transform.RotateAround(cameraFocus.position, Vector3.up, 5 * Time.deltaTime);
         print(transform.rotation.eulerAngles.y);
     }
 }

I need this for synching cursor movement with camera rotation, obviously.

Please, help.

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

2 Replies

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

Answer by Winterblood · Jan 02, 2012 at 03:37 AM

Not clear what this code is trying to do. It looks like you're trying to limit the camera rotation to +/- 5 degrees, which seems a bit small.

Personally, I'd insert a dummy GameObject which sits at the cameraFocus position, with the camera parented to that. Then you just set the dummy's transform.rotation.eulerAngles, rather than doing a RotateAround which will gradually accumulate floating point error. You can then easily clamp the Y rotation to exactly what you want.

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 krides · Jan 02, 2012 at 09:43 AM 0
Share

How do I add those eulerAngles then? What code would you use for that?

avatar image Winterblood · Jan 02, 2012 at 05:05 PM 0
Share

Pretty much what you've got, except you want the rotation script to be on the dummy pivot object ins$$anonymous$$d of the camera. Try this (may have typos, I write in C# usually):

 var maxRot = 45;
 ...
 var angle = transform.rotation.eulerAngles.y;
 angle += Input.GetAxis("$$anonymous$$ouse X");
 if (angle > maxRot)
     angle = maxRot;
 else if (angle < -maxRot)
     angle = -maxRot;
 transform.rotation.eulerAngles = new Vector3( 0, angle, 0 );

NB. You have to set the entire Vector3 - you can't modify transform.rotation.eulerAngles.y on it's own.

avatar image
0

Answer by pset · Jul 25, 2017 at 02:36 PM

You can use Quaternion.RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta) function to restrict rotation:

 Quaternion ClampRotationXByLookDirection(Quaternion curentRotation)
 {
     Vector3 lookDirectionX = Vector3.ProjectOnPlane(lookDirection, Vector3.right).normalized;
     Quaternion lookRotation = Quaternion.LookRotation(lookDirectionX);
     Quaternion towardsRotation= Quaternion.RotateTowards(lookRotation, curentRotation, lookDeltaX);
     return towardsRotation;
 }
 
 Quaternion ClampRotationYByLookDirection(Quaternion curentRotation)
 {
     Vector3 lookDirectionY = Vector3.ProjectOnPlane(lookDirection, Vector3.up).normalized;
     Quaternion lookRotation = Quaternion.LookRotation(lookDirectionY);
     Quaternion towardsRotation= Quaternion.RotateTowards(lookRotation, curentRotation, lookDeltaY);
     return towardsRotation;
 }
 

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

6 People are following this question.

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

Related Questions

Edited mouselook script rotation clamp not working 0 Answers

audiosource.PlayClipAtPoint only plays once 2 Answers

Quaternion - Euler Angles, weird variable swapping when converting 2 Answers

My rigidbody's y rotation is always 180 1 Answer

Camera rotate around a moving object: fixedLateUpdate? 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