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
1
Question by adslitw · Jan 06, 2013 at 11:59 AM · rigidbodyrotatetorque

How to rotate two axes independently, simultaneously

Hi all,

I've done as much searching as possible (and a whole lot of experimenting), but I can't quite seem to solve this problem I'm having. Essentially I want to rotate an object on two axes simultaneously, but independently. I've drawn a quick sketch to show what I mean:

Rotation diagram

Ideally I'd like both rotations to be physics (rigidbody) based, but I'm happy to settle for an approximation using slerps. The closest I've come so far is by using a rotation for the Y rotation:

 transform.RotateAround(transform.position, transform.up, rotateForce);

Whilst using torque for the Z rotation:

 rigidbody.AddRelativeTorque(Vector3.forward * tiltForce);


This works, but is nowhere near robust and the object quickly ends up in strange positions, for example rotated around its X axis (I'd like to avoid any rotation in the X axis if at all possible?).

I'm pretty stumped, so some tips to get me looking in the right direction would be very helpful. I've got this nagging feeling I should be doing something with Quaternions, but I'm not quite sure what! Thanks very much.

rotationdiag.png (29.4 kB)
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
1
Best Answer

Answer by adslitw · Jan 06, 2013 at 04:08 PM

OK, as always (in my case at least), resorting to posting on a forum always guarantees I'll figure it out. For those who come here looking for the answer, here's my solution:

 // Rotate around Y axis
 transform.RotateAround(transform.up,Time.deltaTime * YForce);
     
 // Rotate around Z axis
 transform.RotateAround(Vector3.forward, Time.deltaTime * ZForce);


Place these in update() or fixedupdate() and create the YForce/ZForce variables to be whatever you want (I'm getting input from an Xbox controller).

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
1

Answer by Piflik · Jan 06, 2013 at 02:53 PM

Th usual approach is to separate the rotation. Parent the object to an Empty, rotate that around the global Z-axis and rotate the child around it's local Y-axis.

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 adslitw · Jan 06, 2013 at 03:45 PM 0
Share

Thanks, will look into doing that.

avatar image
0

Answer by Alexander-Perrin · Jan 06, 2013 at 02:28 PM

Why can't you just use transform.Rotate(new Vector3(0, yRotation, zRotation), Space.Self);? That should rotate the transform by 'yRotation' units around the local y-axis and 'zRotation' units around the local z-axis

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 adslitw · Jan 06, 2013 at 02:39 PM 1
Share

Unfortunately when you do that you end up with a combination of the rotations, i.e. the Y rotation does not occur around the Y-vector in the diagram above. I'm still experimenting, so I'll be sure to post an answer if I figure it out. Whatever I do seems to end up with rotations in X axis!

avatar image
0

Answer by IntergalacticSloth · Jul 03, 2017 at 03:47 PM

TL DR

After 16 hours, I got my two axes rotating cleanly. And I did indeed have to use parenting — but even that didn't work for a long time


I just wanted to have code that causes an object to turn in circles and also front flip as it turns. OR IN OTHER WORDS: rotate yaw and pitch without any effect on roll.

Here's what finally worked.

In Unity:

  1. Create an empty. Call it ForYaw.

  2. Give that a child empty, called ForPitch.

  3. Finally, make a cube and move it to z= 5 (forward). Now we can see what we are trying to avoid, which will be twisting the cube (accidental "roll" while we yaw and pitch).

So you have YawObject/PitchObject/Mesh to see. However, the details of the above don't matter much. As long as PitchObject is child, what happens next should work.

Now, using C# in Visual Studio or whatever, do your setup. This goes in Update:

   objectForYaw.Rotate(objectForYaw.up, 1f, Space.World);
         objectForPitch.Rotate(objectForPitch.right, 3f, Space.World);
 
     //this will work on the pitch object as well
     //objectForPitch.RotateAround
     //    (objectForPitch.position, objectForPitch.right, 3f);

That's what worked for me! Good luck!


PS.

Note that it all broke for me when I tried to vary the order of parenting. It also breaks if I change Space.World for the child pitch object.

Yes, I could use Quaternion functions... on the parent. The parent-Yaw object is pretty tough. But Quaternion approaches failed me for the pitch. I really needed Rotate, or RotateAround (applied carefully) .

I'm confused for sure. I could definitely use some clarification on why I had to take a local axis but apply it through world space. Even in my solution, why does Space.Self ruin everything?

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

12 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

Related Questions

add force to rotation 1 Answer

Rotation Force on a model 2 Answers

Rotating a rigid body to face left or right smoothly 4 Answers

how can I rotate object reading from excel data & time steps 3 Answers

Rotate Object to Euler Angle using Physics? 3 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