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 T86 · Mar 07, 2018 at 01:45 PM · scripting problemrotation axisworld space

c# how can I set the rotation of a GameObject to angles in world space

I get different angles from an interface and I like to turn a GameObject in order of these angles. The angles are given in world space.
As everyone can see here, the general code is: transform.rotation = Quaternion.Euler(angles);

But when I like to rotate the object
- 90 degree around the y-axis
- and then around the world-x-axis

the rotation in face is:
- 90 degree around the y-axis
- and then around the world-z-axis (= object-x-axis).

What am I doing wrong and
how can I rotate a gameobject in world space coordinates to any given angles?


Thanks a lot for your help!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by andyborrell · Mar 07, 2018 at 02:10 PM

The rotations performed by Quaternion.Euler are performed in the following order:

  • Z

  • X

  • Y

A simple way to perform rotations in any order you want is to combine multiple calls. So if you want to rotate around the Y axis, and then around the X axis you can make your quaternion like so:

 Quaternion rotation = Quaternion.Euler(xRotation,0,0) * Quaternion.Euler(0,yRotation,0);
 

In multiplication the order of rotations are applied from right to left. So here the y rotation happens first, and then the x.

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 T86 · Mar 07, 2018 at 02:44 PM 0
Share

Thanks a lot for the answer. For my problem I´ve described above your answer is working perfectly.

Normally I don´t know in which direction the object rotates next. Do I have to make a distinction of cases for all rotations or is there any general solution without that?

avatar image andyborrell T86 · Mar 07, 2018 at 02:56 PM 0
Share

I'm not sure I understand your question. Is your problem that you have a UI where you can tweak x,y,z rotation values (like in the Unity inspector) and you want the object to rotate in world space around whatever axis you're currently modifying?

avatar image T86 · Mar 07, 2018 at 03:39 PM 0
Share

The second one: I get any rotation from the interface and I like to rotate my object. Is it necessary to check in which way the rotation has changed or is there any general way?

avatar image
0

Answer by Bunny83 · Mar 07, 2018 at 03:37 PM

From your comments on the other answer i seems you are not interested to set the rotation to an absolute value but to perform a relative rotation. In this case you can simply do:

 transform.rotation = Quaternion.AngleAxis(angle, rotationVector) * transform.rotation;

This will rotate the object relatively around the given rotationVector by "angle" degrees.

So this:

 transform.rotation = Quaternion.AngleAxis(90, Vector3.up) * transform.rotation;

will rotate the object around the world up vector (yAxis) while this:

 transform.rotation = Quaternion.AngleAxis(90, Vector3.right) * transform.rotation;

would rotate around the world xAxis. However if you want you can also pass "transform.right" or any other rotation axis you want to rotate around.

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 T86 · Mar 07, 2018 at 03:49 PM 0
Share

Thanks for your comment. I think I can´t clarify my problem: I get angles form the interface and my object should rotate in these position. I get absolute values (from the world coordinate system), but I explained it with relative rotations just to make sure you understand me. So in my example I get further angles: first: (0, 90, 0), then (45, 90, 0). Important is, that this is just one case. I also get other angles from other axis in other orders. Is there a general way to solve it?

avatar image
0

Answer by T86 · Mar 08, 2018 at 08:47 AM

I´ve tried different options to turn my object in any angles in world space:

  • set the rotation to Quaternion.identity and turn it then in the quaternion order: Z,X,Y

  • set the rotation to Quaternion.identity and turn it with translate.Rotate(Vector3, space.world)

  • rotate it like Bunny83 told in his answer

But with all the solutions I don´t get the correct rotation in world space of my object. I think the problem itself is very simple and a general one, I can´t belief that there is no standard solution.

Can anybody help?

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

137 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

Related Questions

Rotating cube steadily around world axis not working properly 0 Answers

How can I get Rigidbody.MoveRotation to rotate in world space on two axes? 1 Answer

i need to rotate cube in z axis or x axis one direction at time 0 Answers

Why does the value I passed from a different script affects the x axis of the game object even though I inserted the value in the z axis 1 Answer

Rotation via script behaves unexpectedly 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