• 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 /
  • Help Room /
avatar image
0
Question by Dooskington · Jun 23, 2017 at 01:59 AM · rotationphysicsquaternions

Rotate single axis of object to nearest 90 degrees

I'm working on a 6 DOF game, so I have a character that flies around, with mouselook, and rotates properly.

What I want to do now, is if the player isn't rolling (by pressing Q and E), I want to "correct" the roll of the quaternion to the nearest 90 degree angle (so the s$$anonymous$$p levels itself). So, I guess I want to modify the rotation, along the transform.forward axis, to go to the next increment of 90.

It's easy to do if I just modify the eulerAngles, but it doesn't quite have the effect I want - it doesn't take into account the forward vector, so as soon as you turn upside down or at a strange angle, it freaks out and doesn't rotate properly.

Here is what I have now:

     private void HandleMouseLook()
     {
         float mouseInputX = Input.GetAxis("Mouse X") * mouseSensitivityX;
         float mouseInputY = -Input.GetAxis("Mouse Y") * mouseSensitivityY;
         float rollInput = Input.GetAxis("Roll") * rollSpeed; // Roll is Q and E keys
 
         var rotation = new Vector3(mouseInputY, mouseInputX, rollInput) * Time.deltaTime;
         S$$anonymous$$pComponent.RigidbodyComponent.AddRelativeTorque(rotation);

         // If rollInput == 0.0f, correct rotation along transform.forward axis to nearest 90 degrees
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FlaSh-G · Jun 23, 2017 at 07:35 AM

I'd recommend splitting your GameObject into two, and making one of them the parent of the other.

The parent GO would handle the mouse look rotation you already have, w$$anonymous$$le the c$$anonymous$$ld rolls relatively to it. The c$$anonymous$$ld also contains the model, so both rotations get combined for it. Moving the c$$anonymous$$ld's local rotation towards the nearest 90 degree step is rather easy.

Now you can choose: Does rolling affect how the mouse look rotation works, or is it rather just a visual t$$anonymous$$ng? If it's just visual, you can stop here. If not, you will have to read the c$$anonymous$$ld's local rotation and modify your mouse look rotation vector in the parent's script accordingly. Transform.TransformDirection will probably help there.

 var rotation = new Vector3(mouseInputY, mouseInputX, 0);
 transform.TransformDirection(rotation);
 S$$anonymous$$pComponent.RigidbodyComponent.AddRelativeTorque(rotation* Time.deltaTime);

I haven't tested t$$anonymous$$s, but it should give you a direction to go for.

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 Dooskington · Jun 24, 2017 at 01:19 AM 0
Share

Unfortunately this doesn't really work.

It works until the player turns upside down, or at a steep angle - then it starts freaking out. I've tried messing with transform.TransformDirection, but it doesn't seem to do what I want. I think I need a quaternion solution, as it will account for the strange steep angles of the parent object.

avatar image FlaSh-G Dooskington · Jun 24, 2017 at 10:36 AM 0
Share

Maybe this related question helps you.

avatar image Dooskington FlaSh-G · Jun 24, 2017 at 08:21 PM 0
Share

FlaSh-G,

It kind of did! I almost have it. On my child component (the camera), I am doing this now:

         Quaternion rotation = Quaternion.LookRotation(transform.parent.forward, transform.parent.up);
         Vector3 euler = rotation.eulerAngles;
         euler.z = 0.0f;
 
         transform.rotation = Quaternion.Euler(euler);

This seems to keep it correct, while in a normal orientation - but I'm still getting the same issue when I turn upside down, or straight vertically. The definition of it's Z axis seems to change and it just spins around trying to snap itself to 0.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

116 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

Related Questions

Make camera have same rotation but with an offset 0 Answers

Configurable joint don't rotate around the local axis of the rigidbody 0 Answers

Smooth, Frame independent, relative rotation? 0 Answers

How to rotate a child object independently from it's parent so that it maintains it's transform integrity and shape 0 Answers

How to use physics to make an object face a specific direction 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