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 Jankaro · Aug 27, 2019 at 08:14 PM · rotationclampanglesmethodsclamped rotation

Euler angles clamping issue

I'm trying to clamp my player rotation to limitate it's view range. For 'y' axis it rotates the player, and for 'x' axis it rotates the camera. I already have a method that clamp angles but my problem cames when I try to clamp 'y' axis dynamically. Let me explain it. First here is the method:

 float ClampAngle(float angle, float min, float max)
 {
     if (angle < 0f) { angle = 360 + angle; }
     if (angle > 180f) { return Mathf.Max(angle, 360 + min); }
     return Mathf.Min(angle, max);
 }

and how I implement that for the camera rotation:

             camRot = myCam.transform.eulerAngles + new Vector3(-v, 0, 0);
             camRot.x = ClampAngle(camRot.x, -maxCameraRot, maxCameraRot);
             myCam.transform.eulerAngles = camRot;

It works fine and it clamps rotation from -90 to 90. But now I want to clamp player rotation too when press a button. Problem here is that player rotation vary when you move, so it's not that easy like clamp between -90 and 90.

I need to store actual rotation first, then substract 90 for the minimum and add 90 for the maximum. Something like this:

             pjRot = transform.eulerAngles + new Vector3(0, h, 0);
             pjRot.y = ClampAngle(pjRot.y, actualRot - 90, actualRot + 90);
             transform.eulerAngles = pjRot;

It only works well when players rotation is 0, but if it's orientated to the left, right or any other direction it get's a little crazy.

I have been trying to look other posts or make it by myself, but i can not achive what i want. Let me know if i'm doing it wrong or if I need another method. Thanks

Comment
Add comment · Show 4
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 goutham12 · Aug 28, 2019 at 06:17 AM 0
Share

you can also do like the below

float rAngle = 0.0f;

   private void Update()
     {
         rAngle += Input.GetAxis("Horizontal") * rotationSpeed;

         rAngle = $$anonymous$$athf.Clamp(rAngle, -90.0f, 90.0f);

         rNode.rotation = Quaternion.AngleAxis(rAngle, Vector3.up);

   }
avatar image Jankaro goutham12 · Aug 29, 2019 at 05:20 PM 0
Share

It just keeps initial rotation clamp. Even if I'm at 180 degrees it keeps the range between -90 and 90, ins$$anonymous$$d of clamp rotation between 90 and 270. What I need is to clamp rotation plus $$anonymous$$us 90 degrees, regardless it's actual rotation.

avatar image goutham12 Jankaro · Aug 30, 2019 at 04:21 AM 0
Share

in the second line change the clamp values to whatever you want. like

rAngle = mathf.clamp(rAngle,90.0f,270.0f);

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

213 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Object don't rotate correctly 1 Answer

Clamp a 2D object controlled by the mouse 0 Answers

Is this a good way to Clamp a Rotation? 2 Answers

Clamping a camera with weird results 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