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 /
avatar image
0
Question by dom_cobb_22 · Dec 02, 2016 at 06:16 PM · cameracamera rotationcamera controls

Camera rotation X-axis limits (using Right Stick)

Hello. I found many issues with Mouse using for rotation, but a little bit of Stick with problem solved.

Here is an issue:

I want to limit X-axis rotation (vertical). With mouse controlls it is easy: mouse position equal to rotation. With Stick: current rotation + new rotation.

Here is a code:

 public float hRotationSensitivity = 50f;
 public float vRotationSensitivity = 30f;
 public float minAngle = -45.0f;
 public float maxAngle = 45.0f;

 void Update()
     {
         // CAMERA LOOKING
         float angH = Input.GetAxis("RightH") * hRotationSensitivity * Time.deltaTime * 5;
         float angV = Input.GetAxis("RightV") * vRotationSensitivity * Time.deltaTime * 5;
 
 
         // CL -- Rotate horizontal
         Vector3 newRotationH = new Vector3(0, angH, 0);
         transform.localEulerAngles = transform.localEulerAngles + newRotationH;
 
 
         // CL -- Rotate vertical
         Vector3 newRotationV = new Vector3(angV, 0, 0);
 
         //Vector3 totalRotationV = transform.localEulerAngles + newRotationV;
         //totalRotationV.x = Mathf.Clamp(totalRotationV.x, minAngle, maxAngle);
         //transform.localEulerAngles = totalRotationV;
 
         transform.localEulerAngles = transform.localEulerAngles + newRotationV;
         
     }

Camera looking is work perfectly, but has no limits. Code that is commented does not solve my problem. It makes some strange shaked movement.

Please, what I missed.

I want camera like in GTA V. :D

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 dom_cobb_22 · Dec 06, 2016 at 06:29 AM 0
Share

Anyone? Please help

2 Replies

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

Answer by dom_cobb_22 · Dec 08, 2016 at 02:17 PM

Thanks for answers, but I make my own solution. =)

Here it is:

         Vector3 newRotationV = new Vector3(angV, 0, 0);
         transform.localEulerAngles = transform.localEulerAngles + newRotationV;
 
         float x = transform.localEulerAngles.x;
 
         if (x > 45 && x < 315)
         {
             if (x > 45 && x < 50)
             {
                 transform.localEulerAngles = new Vector3(45, transform.localEulerAngles.y, 0);
             }
             else if (x > 310 && x < 315)
             {
                 transform.localEulerAngles = new Vector3(315, transform.localEulerAngles.y, 0);
             }
         }

It possible to change digits to named variables.

However, its absolutely 100% work =)

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 dom_cobb_22 · Dec 08, 2016 at 01:53 PM 0
Share
 if (x > bottomAngle && x < topAngle)
         {
             if (x > bottomAngle && x < (bottomAngle + 5))
             {
                 transform.localEulerAngles = new Vector3(bottomAngle, transform.localEulerAngles.y, 0);
             }
             else if (x > (topAngle - 5) && x < topAngle)
             {
                 transform.localEulerAngles = new Vector3(topAngle, transform.localEulerAngles.y, 0);
             }
         }
avatar image
0

Answer by AdhikS · Dec 06, 2016 at 07:03 AM

try this if(camera angleY is < 45 && camera angleY is > -45 )

{ Vector3 newRotationV = new Vector3(angV, 0, 0); }

Comment
Add comment · Show 10 · 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 dom_cobb_22 · Dec 07, 2016 at 06:36 AM 0
Share

Nope... Doesn't work. Camera moves until max or $$anonymous$$ position and frozen in this.

avatar image AdhikS · Dec 07, 2016 at 07:54 AM 0
Share

ideally it should work.check your camera angles at runtime and if at runtime your camera angle exceeds 45 or not. i think camera stop working coz angle of camera exceeds 45.check if this is the case

avatar image dom_cobb_22 · Dec 07, 2016 at 08:49 AM 0
Share

I made a mistake (you too). Typed 'Y' ins$$anonymous$$d 'X'... and used localEuler and usual Euler both ins$$anonymous$$d one of this. But now, after correcting, camera not moving at all. (Vertical)

Code looks like this:

         float angH = Input.GetAxis("RightH") * hRotationSensitivity * Time.deltaTime * 5;
         float angV = Input.GetAxis("RightV") * vRotationSensitivity * Time.deltaTime * 5;
 
 
         // CL -- Rotate horizontal
         Vector3 newRotationH = new Vector3(0, angH, 0);
         transform.localEulerAngles = transform.localEulerAngles + newRotationH;
 
 
         // CL -- Rotate vertical
 
         if (transform.localEulerAngles.x < maxAngle && transform.localEulerAngles.x > $$anonymous$$Angle)
         {
             Vector3 newRotationV = new Vector3(angV, 0, 0);
             transform.localEulerAngles = transform.localEulerAngles + newRotationV;
         }
         
         Debug.Log("transform.localEulerAngles " + transform.localEulerAngles);

Log message is here: transform.localEulerAngles (359.8, 357.8, 0.0)

It is '359.8' value. Not '-0.2' We are using $$anonymous$$Angle = -45 and maxAngle = 45

So what we have to do?

avatar image AdhikS · Dec 07, 2016 at 09:36 AM 0
Share

sorry for the last mistake try $$anonymous$$ angle 315 ins$$anonymous$$d of -45

avatar image AdhikS · Dec 07, 2016 at 10:14 AM 0
Share

check this link http://answers.unity3d.com/questions/1087351/limit-vertical-rotation-of-camera.html i hope this will solve your issue

avatar image dom_cobb_22 AdhikS · Dec 08, 2016 at 07:48 AM 0
Share

Trying to do like in a link. Camera moves from 0 to 45 just wonderful, but then I turn from 45 to 0 and I reach 0, its reset to 45 (face to floor). I absolutely not understand how two same copy of code may behave differently.

         // Try clamping after apply rotation
         //transform.localEulerAngles = new Vector3($$anonymous$$athf.Clamp(transform.localEulerAngles.x, $$anonymous$$Angle, maxAngle), 0, 0);
 
         if (transform.localEulerAngles.x > maxAngle)
         {
             transform.localEulerAngles = new Vector3(maxAngle, 0, 0);
         }
         else
         {
             if (transform.localEulerAngles.x < $$anonymous$$Angle)
             {
                 transform.localEulerAngles = new Vector3($$anonymous$$Angle, 0, 0);
             }
         }

I try commented part first, then second part (uncommented now). Result is a same.

avatar image dom_cobb_22 dom_cobb_22 · Dec 08, 2016 at 08:21 AM 0
Share

localEulerAngles never uses values with $$anonymous$$us sign. Therefore then it is reach zero and became 359.9, that is going to reset to 45.

Show more comments
Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

third person camera 2 Answers

How can i make my camera rotate/move relative to my players rotation? 0 Answers

VR Disable Camera rotation / tracking 0 Answers

How Do I Make The Camera Orbit Stop at a Certain Point? 0 Answers

How to make character head face in the direction of where the mouse is pointing in FreeFormCameraRig? 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