• 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 Double-V · Apr 04, 2017 at 11:35 AM · camerarotationeuleranglesclampclamped rotation

Bizzare problem with limiting camera veritcal rotation

My game is a first person shooter, where a camera is a child of my player object. Horizontal rotation is applied to the player object, vertical rotation is applied to the camera.

This is what handles cam rotation:

 float x_rotation = Input.GetAxisRaw("Mouse Y");
 Vector3 cam_rotation_updown = new Vector3(x_rotation, 0f, 0f) * look_sensitivity;
 
 cam.transform.Rotate(-cam_rotation_updown);
 
 float x = cam.transform.localEulerAngles.x;
 
             if (x > 60 && x < 300)
             {
                 if (x > 60 && x < 180)
                 {
                     cam.transform.localEulerAngles = new Vector3(60, 0, 0);
                 }
                 else if (x < 300 && x > 180)
                 {
                     cam.transform.localEulerAngles = new Vector3(300, 0, 0);
                 }
             }

But the problem is if I move my mouse fast enough it'll break from the limitation of rotation that those if statements create. Then my camera ends up backwards and strangely enough its rotation isn't somewhere around 180 degrees, but around 0, which technically checks out as the correct range, but in reality it's not.

EDIT: I've noticed that when the teleport happens, y and z rotation changes from 0 to 180 instantly.

I attach this picture to clear things up: alt text

rotation-explained.png (102.5 kB)
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
0
Best Answer

Answer by Double-V · Apr 04, 2017 at 12:10 PM

Allright, I think I've fixed it:

 float x_rotation = Input.GetAxisRaw("Mouse Y");
 Vector3 cam_rotation_updown = new Vector3(x_rotation, 0f, 0f) * look_sensitivity;
  
  cam.transform.Rotate(-cam_rotation_updown);
 
 float x = cam.transform.localEulerAngles.x;
             float y = cam.transform.localEulerAngles.y;
             float z = cam.transform.localEulerAngles.z;
 
             if (x > 60 && x < 300)
             {
                 if (x > 60 && x < 100)
                 {
                     cam.transform.localEulerAngles = new Vector3(60, 0, 0);
                     if (y == 180 && z == 180)
                     {
                         cam.transform.localEulerAngles = new Vector3(60, 0, 0);
                     }
                 }
                 else if (x < 300 && x > 260)
                 {
                     cam.transform.localEulerAngles = new Vector3(300, 0, 0);
                     if (y == 180 && z == 180)
                     {
                         cam.transform.localEulerAngles = new Vector3(300, 0, 0);
                     }
                 }
             }
 
             if (y == 180 && z == 180)
             {
                 if (x > 0 && x < 60)
                 {
                     cam.transform.localEulerAngles = new Vector3(60, 0, 0);
                 }
                 else if (x < 360 && x > 300)
                 {
                     cam.transform.localEulerAngles = new Vector3(300, 0, 0);
                 }
             }


Since the y and z rotation was changing to 180 I just added a bunch of checks for this. If you want to have a wider view just change the values, eg. if you want to look 70 degrees up and down change all the sixties to 70 and all the three hundreds to 290. Leave the 0-s and 180-s alone.

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

Answer by VeeooshL · Apr 04, 2017 at 11:38 AM

You could clamp the values if they are over the specified amount. Example:

 if(angle > 5){
    angle = 5
 }

EDIT: could also use this - https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

100 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

Related Questions

Limit Rotation of physics object in 2D 1 Answer

Edited mouselook script rotation clamp not working 0 Answers

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

Limit gameobject rotation to -480 and 480 degrees? 2 Answers

Rotating and orbiting an game object with a stop angle 0 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