Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 matta9001 · Jun 24, 2015 at 04:44 PM · c#mathclamppicture

Clamping between everything but the min/max values

I have an object, and i want to clamp the z value between -90, and 90. So it has a total of 180 degrees it can go. This is my problem, when in play mode, it is no longer -90, and it is 270. So i need to clamp my angle from 90, to 270, mathematically this is from 90 to 270, but i want it to include 0 to 90, and 270 to 360 its hard to explain so i made a picture right here : http://puu.sh/iB8Lo/f18dede505.png

here is my code in case i'm doing something wrong

 Quaternion clamp = Quaternion.Euler(transform.localEulerAngles.x, transform.localEulerAngles.y, Mathf.Clamp(transform.localEulerAngles.z, -90, 90));
 transform.rotation = clamp;

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 troien · Jun 24, 2015 at 05:11 PM 0
Share

Well, something like this might work. This should make your z angle always between -180 and 180 before clamping.

 Quaternion clamp = transform.localEulerAngles;
 
 while (clamp.z >= 180f)
     clamp.z -= 360f;

 while (clamp.z < -180f)
     clamp.z += 360f;
 
 clamp.z = $$anonymous$$athf.Clamp(clamp.z, -90, 90);
 
 transform.rotation = clamp;

1 Reply

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

Answer by Dave-Carlile · Jun 24, 2015 at 04:59 PM

Haven't tested this, but it might/should work? The basic idea is that if z

 float z = transform.localEulerAngles.z;
 z = Mathf.Clamp((z <= 180) ? z : -(360 - z), -90, 90);
Comment
Add comment · Show 3 · 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 matta9001 · Jun 24, 2015 at 05:09 PM 0
Share

This works, i feel bad because i just pasted it in to my code and it just works, so thank you very much. But i'm pretty new to coding and haven't seen all the ? and : things. Is it like a compact if statement? $$anonymous$$abye link me to where i could read about this?

avatar image Dave-Carlile · Jun 24, 2015 at 06:04 PM 0
Share

It's called the ternary operator or conditional operator. It's more or less the same as writing...

... if (z

But it's nice sometimes to be able to do it inline ins$$anonymous$$d of a separate if statement.

avatar image jc538219 · Jun 19, 2020 at 06:18 AM 0
Share

I am not sure if i am late to respond to this. the code is working fine and thanks for this. the only question i have is if the angles are from 0-360 how come its working with -45 to 45.

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Comparing lerping quaternion 1 Answer

Programmatically specifying a random point in an arc in front of the player 1 Answer

What is the mathematical formula for GUI dragging ? 2 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