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 AlienNova · Apr 26, 2016 at 06:27 PM · c#quaternionclampcamera rotationclamped rotation

Clamping camera rotation

I have this bit of code here, huge thanks to Holistic3D, to control camera rotation for looking around.

 void Update () {
     Vector2 md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
 
     md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
     smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
     smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
     mouseLook += smoothV;
     Quaternion rotate = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);

     //My addition
     if (rotate.x > 0) {
         rotate.x = Mathf.Clamp (rotate.x, 270f, 360f);
     } else {
         rotate.x = Mathf.Clamp (rotate.x, 0f, 90f);
     }//if & else
     //End my addition

     transform.localRotation = rotate;//Rotates the camera up & down
     character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);//Rotates the player & camera, left & right
 }//Void Update

The only problem I have with it is that it doesn't clamp the cameras X rotation. And trying to clamp rotation is extremely weird. And when I try to clamp the rotation it clamps it to only 270 degrees, or 90 degrees.

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

1 Reply

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

Answer by AlienNova · Apr 26, 2016 at 06:51 PM

I found the answer myself. I'm still new to Unity so I didn't realize you could simply use a negative float in rotation for clamping! Final code if anyone is interested!

 void Update () {
     Vector2 md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
 
     md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
     smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
     smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
     mouseLook += smoothV;
 
     transform.localRotation = Quaternion.AngleAxis (-Mathf.Clamp(mouseLook.y, -90, 90), Vector3.right);//Rotates the camera up & down
     character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);//Rotates the player & camera, left & right
 }//Void Update
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 Margallo · May 13, 2017 at 07:31 AM 0
Share

Hi AlienNova!

Your code is so great but im working in mobile device how can i set it in button mode?I'm using event trigger. when the button is down and tap the screen the camera goes move. how can i set the camera to zero when the button is down? please help me i i'm new to unity and coding ! :)

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

150 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 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

Rotation before 0 and after 360 0 Answers

Swapping Right handed Quaternions to left handed from streaming data. 1 Answer

Clamp a rotation in unity 0 Answers

Object don't rotate correctly 1 Answer

Camera clamps to the world scale, not local :c 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