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 ThisGuyThatsHere · Apr 25, 2016 at 06:01 PM · rotationvector3quaternioneuler

Problem with rotation around axis

         if(transform.up == Vector3.up) {
             g.localRotation = Quaternion.Euler(new Vector3(0f, 90f * GD.rot, 0f));
 
         } else if(transform.up == Vector3.down) {
             g.localRotation = Quaternion.Euler(new Vector3(180f, 90f * GD.rot, 0f));
 
         } else if(transform.up == Vector3.right) {
             g.localRotation = Quaternion.Euler(new Vector3(90f * GD.rot, 0f, -90f));
 
         } else if(transform.up == Vector3.left) {
             g.localRotation = Quaternion.Euler(new Vector3(90f * GD.rot, 0f, 90f));
 
         } else if(transform.up == Vector3.forward) {
             g.localRotation = Quaternion.Euler(new Vector3(90f,  0f, 90f * GD.rot));
 
         } else if(transform.up == Vector3.back) {
             g.localRotation = Quaternion.Euler(new Vector3(-90f, 0f, 90f * GD.rot));
 
         }

So, I want to rotate object g in the direction my object is pointing (one of 6 basic ones) with it's "up". At this point everything is fine. Also I want to rotate it around its local "y" (90f x GD.rot) and its still all fine with first 4 sections, but 5th and 6th works wrong. My 90f x GD.rot used in the "z" slot rotates the object around "y" axis instead of "z" ("y" is also rotating it around "y") and Im really confused whats wrong, maybe its a 5.4b problem, or something?

Comment
Add comment · Show 1
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 tanoshimi · Apr 25, 2016 at 06:27 PM 0
Share

I believe you're either encountering gimbal lock, or else you're confused as to the order in which x,y,z rotations are applied, but your code is a bit unclear (what is GD.rot?).

If you were to imagine a dice, to go from "forward" to "up" would require a rotation of 90' in a single (X) axis, right? But you seem to be applying deltas to all three axes between those states. Can you paste a screenshot of your object showing what you believe to be the incorrect rotations?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Apr 25, 2016 at 06:43 PM

This seems like a needlessly complicated solution to a very simple problem.

What you're encountering is Unity's order of operations when converting Quaternion rotations to Euler angles. Specifically, they are processed by the Z rotation, followed by X, followed by Y.

Therefore, on your last pair of examples, rather than rotating by 90f or -90f, followed by (90f * GD.rot), you're instead doing it the opposite way. Then, if you move that to the Y-axis rotation instead, it's done in the opposite order. By coincidence, you wind up with the same rotation.

But, again, this seems like quite an overwhelming solution to a reasonably simple problem.

From the looks of it, you should be able to build this rotation like this instead:

 g.rotation = Quaternion.AngleAxis(90f * GD.rot, Vector3.up);

In other words, rather than trying to set the local rotation to counteract the parent's(?) rotation, you could set the global rotation instead and cut out the middle man.

That said, if you're dead set on modifying the local rotation, you will instead need to break the rotation apart into two stages to make it foolproof. First, you can set the current local rotation to the inverse of the main rotation (i.e. your 90f and -90f), then apply the secondary rotation of (90f * GD.rot).

 g.localRotation = Quaternion.Inverse(transform.rotation) * Quaternion.Euler(0, 90f * GD.rot, 0);
 // or...
 g.localRotation = Quaternion.Inverse(transform.rotation) * Quaternion.AngleAxis(90f * GD.rot, Vector3.up);

Either way, it can simplified into a single process rather than a 6-part, strictly cardinal-direction-based if statement.

Edit: For what it's worth, once you break the rotation process apart into multiple stages to avoid running into issues with the order of operations, you'll always wind up with the same results; one rotation to undo the parent's rotation will result in the following rotation being axis-aligned with global coordinates, so the second rotation would always be able to be the same.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Vector3.forward only moves me in 2 directions when rotating 1 Answer

Quaternion.Euler rotations don't add up 0 Answers

Turret on Tank rotating fine until I'm not flat on ground 1 Answer

Why is Quaternion.Eular only rotating in one direction? [Answered] 1 Answer

Connecting transform with Vector3 4 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