• 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 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 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 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.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

221 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

Related Questions

Unity 5 Car Audio script issues 0 Answers

Windows 10 won't go to sleep while Unity is open 3 Answers

not login to unity forum 0 Answers

How to rotate CharacterController on slope 0 Answers

Unity wont let me select an external editor 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges