Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Balphagore · May 07, 2020 at 06:22 AM · unity 5issuetransform.rotationquaternionsxyz

How to compensate for rotation along the Z axis while rotating along the X and Y axes

I'm trying to implement jetpack control in space, which implies complete control over movement and rotation. I’ve already figured out why this problem arises and tried different options, but not one gave the desired result. The scene consists only of GO with a camera attached to it and several cubes for orientation in space. The script itself is now like this:

 using UnityEngine;
 public class Rotation : MonoBehaviour
 {
     private void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
     private void Update()
     {
         Quaternion roll = Quaternion.AngleAxis(Input.GetAxis("Roll"), Vector3.forward);
         Quaternion pitch = Quaternion.AngleAxis(Input.GetAxis("Pitch"), Vector3.right);
         Quaternion yaw = Quaternion.AngleAxis(Input.GetAxis("Yaw"), Vector3.up);
         this.transform.rotation = this.transform.rotation * roll * pitch * yaw;
         Vector3 direction = new Vector3(0, 0, 0);
         direction += Camera.main.transform.forward * Input.GetAxis("Forward");
         direction += Camera.main.transform.right * Input.GetAxis("Right");
         direction += Camera.main.transform.up * Input.GetAxis("Up");
         transform.gameObject.GetComponent<Rigidbody>().velocity = direction*10;
     }
 }

It allows you to freely control the object with the camera, but when rotation occurs simultaneously along the X and Y axes, the roll along the Z axis also occurs. I tried different ways to remove this, but failed. I can easily remove rotation on the Z axis. By rotating in Space.Self and Space.World, or by spacing the different axes in the parent and child, or simply undoing all the rotation on the z axis. But here is the problem, I need this rotation on the Z axis when I do the roll. And it is not necessary when it arises from rotation along the axes X and Y

I tried doing this:

 using UnityEngine;
 public class RollPitchYaw : MonoBehaviour
 {
     private float currentZ;
     void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
     private void Update()
     {
         Quaternion roll = Quaternion.AngleAxis(Input.GetAxis("Roll"), Vector3.forward);
         Quaternion pitch = Quaternion.AngleAxis(Input.GetAxis("Pitch"), Vector3.right);
         Quaternion yaw = Quaternion.AngleAxis(Input.GetAxis("Yaw"), Vector3.up);
         if (Input.GetAxis("Roll") != 0)
         {
         this.transform.rotation = this.transform.rotation * roll * yaw * pitch;
         currentZ = transform.eulerAngles.z;
         }
         else
         {
             float z = transform.eulerAngles.z;
             Quaternion counterRoll = Quaternion.Euler(0, 0, -z);
             this.transform.rotation = this.transform.rotation * yaw * pitch * counterRoll;
             Quaternion originRoll = Quaternion.Euler(0, 0, currentZ);
             this.transform.rotation = this.transform.rotation * originRoll;
         }
 
         Vector3 direction = new Vector3(0, 0, 0);
         direction += Camera.main.transform.forward * Input.GetAxis("Forward");
         direction += Camera.main.transform.right * Input.GetAxis("Right");
         direction += Camera.main.transform.up * Input.GetAxis("Up");
         transform.gameObject.GetComponent<Rigidbody>().velocity = direction*10;
     }
 }

But when the angle is 90 or 270 degrees along the Z axis, a strange revolution occurs. Who can advise what in this situation?

Comment
Add comment · Show 3
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 Balphagore · May 05, 2020 at 01:22 PM 0
Share

BTW I use my own axes from Input $$anonymous$$anager, so the code will not work without them.

avatar image aoisenshi · Jan 21 at 09:35 PM 0
Share

I finally come across a post describing the exact same issue I've been trying to resolve for days and unfortunately there are no replies! Wondering if you ever happened to find a fix for this?

avatar image Balphagore aoisenshi · Jan 22 at 03:59 PM 0
Share

Hello, I didn't find a solution to the problem, but I noticed that other serious games don't have it either. For example, Space Engineers and Prey also have this problem.

0 Replies

· Add your reply
  • Sort: 

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

222 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Issue adding UI Button Unity 5 1 Answer

Unity 5 Global fog with transparency issues 2 Answers

4 rotations WTF 1 Answer

[Solved] Issues knowing if an object is moving "forward" or "backwards" (C#) 2 Answers

Help! Sounds won't play when changing scene 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