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 Dan1994 · Oct 05, 2019 at 12:06 PM · rotationeuler angles

Read rotations around local axis

Hi! I'm new with Unity and I may need some help for a project.

In my work I need to get the angle of rotation of an object around one of its own axis. I know that EulerAngles gives me the rotation angles in world coordinates and localEulerAngles in parents coordinates. But i really need to know the angle of rotations in the object reference frame.

Is there a way? Thanks

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by WarmedxMints · Oct 05, 2019 at 01:18 PM

It sounds like you want to know how much it has rotated from its initial rotation. You could store the start rotation and then calc how much it has moved since, like so;

     private Quaternion _referenceRot;
 
     private void Start()
     {
         _referenceRot = transform.rotation;
     }
 
     private void Update()
     {
         Debug.Log(RotationOnAxis(1,transform.rotation * Quaternion.Inverse(_referenceRot)));
     }
 
     //axis 0 = x, 1 = y, 2 = z
     private float RotationOnAxis(int axis, Quaternion rot)
     {
         rot.x /= rot.w;
         rot.y /= rot.w;
         rot.z /= rot.w;
         rot.w = 1;
 
         return 2.0f * Mathf.Rad2Deg * Mathf.Atan(rot[axis]);
     }
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 Dan1994 · Oct 05, 2019 at 02:10 PM 0
Share

Thanks! But still is not what I'm looking for! I see that it can be difficult to understand just from what i wrote so I'll make an example:

I have two cubes, one is parent and the other is child. Now I rotate the child of 45 degrees around its y axis and 30 degrees around its z axis. Now I can see that its localEulerAngles are (0,45,30), respect to parents axis.

If now I start rotating around the x axis of the child, of course all the localEulerAngles vary. What I want is to read that my child object has a rotation of 45 around its y and 30 around its z and tot. around its x.

avatar image Dan1994 · Oct 05, 2019 at 02:12 PM 0
Share

I think it can be done using quaternions, but i don't know how to use them.

avatar image Amitloaf · Nov 29, 2020 at 10:05 AM 1
Share

This one really helped me! Thank you, it was very useful! Which math is this based on? (Is it a rotation matrix?)

avatar image
1

Answer by Bunny83 · Oct 05, 2019 at 12:48 PM

The way you have it worded at the moment doesn't make much sense. An object's position / rotation in it's own local reference frame is always 0,0,0 since that's what the local coordinates actually represent. So an object is always located at the local position 0,0,0 and has a rotation( euler angles ) of 0,0,0.


So what you want just makes no sense. I'm sure you actually want the rotation relative to some other reference space. However from your question it's not clear what that might be. You may want to draw yourself your coordinate system(s) on a piece of paper and just clear up for your self which angle you are interested in. So if you draw reference lines which angles you want to read.

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 Dan1994 · Oct 05, 2019 at 12:56 PM 0
Share

Yes, you are right. I think I didn’t explain what I meant really well. When my object rotated along its own y axis I want to read that rotation because I need to use that angle. However, if I use localEuler angles, for example, the value I’m reading on the y value of that angle can vary even if I rotate along some other axis of the object because even a rotation along x and z axis of my object can produce a rotation around y axis of the parent.

So it is not the angle I’m looking for .

avatar image
1

Answer by ZeBarba · Oct 06, 2019 at 08:42 PM

You want to use Quaternion.Angle. This calculates the angles between to rotations.

As @WarmedxMints suggested, you will have to create a variable to store your reference rotation, and use Quaternion.Angle to find the angle between your current rotation and last rotation.

But you will only get the "pure" euler angle of an axis if you do this between each rotation.


I created a sample scene to illustrate:

  • There are two cubes, one of them is a child to the other. The bottom one is the child.
    The parent starts with a rotation in all axis.

alt text

After that, I rotate the child 45 degrees in y and 30 degrees in z, print on the console the eulerAngles and localEulerAngles, and store this rotation in a variable: alt text

On the next step, I rotate the child 30 degress in x, and print the Quaternion.Angle result, and the euler angles (local and global).

Link: https://ibb.co/3d55MPF (there a limit for attachments)

As you can see, the Quaternion.Angle return the angle between the two rotations.

The thing is: this method allows you to find the angle one rotation at a time. If you do several rotations at once, this will not work.


2019-10-06-17h30m-1.png (154.8 kB)
2019-10-06-17h29m.png (140.1 kB)
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 Dan1994 · Oct 06, 2019 at 10:16 PM 0
Share

Thanks, I'm not still sure is the best way, but it can be helpful

avatar image
0

Answer by Dan1994 · Oct 06, 2019 at 10:24 PM

To be even more clear I'll explain my final purpose:

I have an object, let's call it controller, free to rotate around its axes. I have three more object and each one of them has to rotate around one of its axis.

When the controller rotates around its own X axis the first object rotates around its own X axis. When the controller rotates around its own Y axis the second object rotates around its own Y axis. When the controller rotates around its own Z axis the third object rotates around its own Z axis.

Each object has to move only with the rotation around the right axis and never with others. It's like I want to divide the transform.rotation of the controller in three different objects.

Comment
Add comment · Show 4 · 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 ZeBarba · Oct 07, 2019 at 01:27 AM 0
Share

In that case, I would not parent any of them. Just put a script on the "main" one, with a reference to the 3 "little" ones. And something like this:

 transform.rotation = transform.rotation * Quaternion(30,Vector3.right)
 littleOne1.transform.rotation = littleOne1.transform.rotation * Quaternion(30.Vector3.rigth)


 transform.rotation = transform.rotation * Quaternion(30,Vector3.up)
 littleOne2.transform.rotation = littleOne2.transform.rotation * Quaternion(30.Vector3.up)


and so on. You manipulate the rotation with Quaternion.AngleAxis separately, in all algles for the main one, and just for one angle for the little ones.

Insted of 30 you put your input there.

avatar image Dan1994 ZeBarba · Oct 07, 2019 at 08:22 AM 0
Share

Ok, but what if I don't know that "30" input? That's exactly the value I want to read from the controller!

avatar image ZeBarba Dan1994 · Oct 07, 2019 at 10:57 AM 0
Share

One way is my answer post. Calculate the delta rotation of the main object each frame and apply it to the little objects, as respect to the axis you want to.

Any more than that, depends on your code. For instance, how is the main object rotating? Depending on it, you could do the same thing on the little ones, but only for one axis.

$$anonymous$$y last suggestion is: Stop using euler angles. Study and understand quaternions, it will make your life easier. Using Quaternions.AngleAxis, Quaternion.LookRotation, Quaternion.RotateTowards, and a good understanding of Vector Arithmetic (dot product, usage of normals, etc) can solve the vast majority of situations.

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

149 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

Related Questions

Smoothly rotating a gameObject 90 degrees on a button press and being able to rotate it in the opposite direction off of the same button. 0 Answers

Euler angle problems with rotation limit script 3 Answers

Rotation relations and EulerAngles's mess 0 Answers

,How to clamp my turret rotation? 1 Answer

Difference between transform.localEulerAngles and transform.localRotation=Quanerion.Euler 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