Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Luke 4 · Apr 17, 2011 at 12:40 PM · rotationeulerangleslocallimit

Limit local Rotations on one axis

Hi,

i got problems with limiting rotation of cylinder on one axis. I tried almost everything from eulers to quaternions, but i am sure it is only my lack of experience why i am not able to pull this out.

I managed to limit its rotations on X axis, but strangely same code doesnt work for Z axis. Here is the code i got for limiting the X axis.

I know eulerAngles shouldnt be used for setting values separately, but this is only solution that worked for me on X axis.

// Limit rotations

if(Input.GetButton("down")) { if(transform.rotation.eulerAngles.x > 89) { transform.rotation.eulerAngles.x = 89;

 }

} else if(Input.GetButton("up")) { if(transform.rotation.eulerAngles.x < 50) { transform.rotation.eulerAngles.x = 50;

 }

}

Thanks in advance.

Luke

Comment
Add comment · Show 6
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 Owen-Reynolds · Apr 17, 2011 at 02:49 PM 0
Share

What are you trying to do? Let a rigidbody bounce around without turning? $$anonymous$$ake sure a bouncing rigidbody is never too far from straight up? Use up/down to spin an object? Also, from the manual: "Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; )"

avatar image Luke 4 · Apr 17, 2011 at 03:52 PM 0
Share

Sorry, i should explain it better. I want to rotate cylinder by user input at pivot point, not spin. I know i shouldnt use eulerAngles, but this was only solution i was able to limit rotations with.

avatar image Owen-Reynolds · Apr 17, 2011 at 11:45 PM 0
Share

rotate is spin. You can rotate end-over-end, like a propeller or like a top. You can use Euler angles, but read the examples. Searching "keys rotate" here gives lot of hints, depending on what you want.

avatar image Luke 4 · Apr 20, 2011 at 09:05 AM 0
Share

well, as i said, i need to limit the rotation (spin). As you can see in the code i have above, i need to limit it between angle 50 and 90 on X axis. This solution works for X axis, while i know its not good solution as i shouldnt use eulerAngles for this, but i dont know how else do it.

avatar image Luke 4 · Apr 20, 2011 at 09:05 AM 0
Share

Problem is that while i rotate the cylinder in scene view the values of rotation goes from 0 ( default rotation ) to plus and $$anonymous$$us values. So in scene view when i test it, i can tell that i need to limit it between say 30 and -30 on z axis but when the game runs the rotation doesnt gets $$anonymous$$us values, its getting only 0 - 360 values so the negatie value i tested in scene view is now 330-360 ( the original 0 - (-30)). So thats another problem i dont understand why is appearing.

Show more comments

3 Replies

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

Answer by Owen-Reynolds · Apr 22, 2011 at 05:43 PM

It's perfectly fine to set rotation using the classic three X/Y/Z rotations (the ones you see in the Editor for rotation, now called Euler angles.) The rule is you have to set them all at once, or else funny things can happen.

To do this, best to have separate vars for the rotations. After you set the euler angles, Unity may convert -10 to 350, or other odd things. If you treat your locals as the "Master copy", it won't matter.

float facing=0, spin=0; // spin is local zRotation, like on a bullet

// somewhere in Update, or FixedUpdaye:

// many ways to set facing and spin. Using arrow keys for a quick test: facing += Input.GetAxis("Horizontal")*120*Time.deltaTime; // standard way to spin at 30 degrees/second: spin += Input.GetAxis("Vertical")*30*Time.deltaTime;

// limit some number: spin = Mathf.Clamp(spin, -30, 30); // the cool kids' way if(spin>30) spin =30; // normal persons' way if(spin<-30) spin =-30;

// this is how the Unity docs say to safely set euler rotations: transform.rotation = Quaternion.Euler(0,facing,spin);

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 Luke 4 · Apr 23, 2011 at 10:55 AM 0
Share

amazing, thanks.Works great. I tried to figure this out myself, but were lost. The trick seems to be in setting each rotation as separate variable, feeding in the axis input and clamp that value. Again, many thanks.

avatar image
1

Answer by bardeov · Apr 20, 2012 at 03:39 PM

I fix your script for rotate limiting, but you can´t add numbers lower than 1 and higher than 359 because it is in degrees.


private var firsEnd = false; private var secondEnd = false;

function OnGUI(){ firsEnd = transform.rotation.eulerAngles.y > 359; secondEnd = transform.rotation.eulerAngles.y < 1; }

function Update(){

if (firsEnd) transform.rotation.eulerAngles.y = 359; if (secondEnd) transform.rotation.eulerAngles.y = 1; }


Sorry for my bad englis. I´m from Czech

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 mthicke2 · May 15, 2013 at 05:59 AM

for anyone else trying to do this - the easiest way is to use the Unity Rotation Constraint script - Add Component>Script>Rotation Constraint

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Limit local rotation 6 Answers

How do you combine local and planar translation? 1 Answer

Rotating Object on its Local World Axis 4 Answers

Rotation Limit 2 Answers

eulerAngles.y always returns 179.9999 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