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
3
Question by Tahj · May 20, 2012 at 12:42 AM · rotationposition

Check an Objects Rotation

Hello,

I know there is a way to check the position of an object along x, y and z, but is it possible to check an objects rotation along x, y and z also? My intention for this is to rotate a cube by 90 degrees along the y axis and to check its rotation using an if statement to trigger an event. I have placed an example of what I would like to do below.

 if(target is rotated at 90 along y-axis)
    //do something
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
5
Best Answer

Answer by kolban · May 20, 2012 at 02:43 AM

An object can be rotated "around" various axis but isn't rotated "along" an axis (just a small nit). Given an object, you can access its Transform properties. One of the transform properties is a property called "rotation" which is the object's rotation defined as a "quaternion". A quaternion object has a property called "eulerAngles" which is a Vector3 representing the objects rotation around the x, y and z axis.

So ... given an object called MyObject ... the following can be used:

 if (myObject.transform.rotation.eulerAngles.y == 90)
 {
    // Do something
 }
Comment
Add comment · Show 7 · 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 aldonaletto · May 20, 2012 at 03:06 AM 1
Share

There's also an eulerAngles property in Transform, transform.eulerAngles - it's the same as transform.rotation.eulerAngles

avatar image kolban · May 20, 2012 at 03:26 AM 0
Share

Thank you Aldo ... excellent addition.

avatar image Kevin_C · May 20, 2012 at 03:55 AM 3
Share

it should be noted that either transform.eulerAngles, and transform.rotation.eulerAngles is prone to being slightly inaccurate by sometimes as much as a full degree. so when testing against a eulerAngle it is best to do so in a range of values (so for this example ((y>89)&&(y<91))

for an example of this create a controllable object, and only rotate it about a given axis. you should notice that the values of the other axis are changing as well (this is due to converting a euler angle to a quaternion to rotate the quaternion, and then converting the quaternion back to euler angles to display) the same will happen when you purely modify the quaternion by a fixed angle (unless you are doing so purely through the inspector then the math flow of the engine differs slightly, and is more precise).

avatar image ImPHL1 Kevin_C · May 20 at 12:08 PM 0
Share

thank you very much, this solved my problem. i was rotating an object by 90 degrees and then checked whether the object has a certain rotation. even though the rotation in the inspector was correct but the value in the code was different(i ws using eulerAngles) and adding this to the code solved my problem.

avatar image Tahj · May 20, 2012 at 06:32 PM 1
Share

Thanks to all of you for your help, it provided me with all that I needed to get this listeners working and learn more ter$$anonymous$$ology as well. Also thanks $$anonymous$$evin_C for mentioning the margin for error as it possibly saved me a lot of possible script debugging.

avatar image AndreasC · Feb 10, 2014 at 02:00 AM 0
Share

@$$anonymous$$evin_C , thank you, been sitting with this issue for one hour now !

Show more comments
avatar image
1

Answer by ShawnFeatherly · Sep 08, 2017 at 12:52 AM

While angle decreasing

Mathf.DeltaAngle() tells you the angular distance between two Euler axis. Saving the deltaAngle from the last frame enables checking if the delta is decreasing. This works great if something is continually rotating along a single axis at fast speeds (less than 180 degrees a frame).

 public IEnumerator RotateTo90()
 {
 float deltaAngle = 360;
 bool isAngleDecreasing;
 do
 {
     float lastDeltaAngle = deltaAngle;

     var amountToRotate = angularSpeed * Time.deltaTime;
     centricTransform.Rotate(Vector3.up, amountToRotate);
     deltaAngle = Mathf.Abs(Mathf.DeltaAngle(this.rotation.eulerAngles.y, 90));
     isAngleDecreasing = deltaAngle < lastDeltaAngle;

     yield return null;
 } while (isAngleDecreasing);
 }

Alternate Quaternion.Dot method

I couldn't get this method to work well. It is likely more performant. It may work for your scenario. Quaternion.Dot can be used to see how close two rotations are to one another. It returns 1 or -1 when the rotations coincide and 0 when they're opposite directions.

 Quaternion targetAngle = Quaternion.Euler(0, 90, 0);
 float precision = 0.9999f;
 if (Mathf.Abs(Quaternion.Dot(this.transform.rotation, targetAngle)) > precision)
 {
     // do something
 }


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

10 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

Related Questions

Positions to rotations of a regular GO 2 Answers

How can I make a First Person Character Controller a child of a moving platform without it affecting the rotation of the Character Controller. 1 Answer

Relative Rotation 1 Answer

How to change an objects position and rotation inside a script? 0 Answers

Unable to set Rotation 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