- Home /
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?
BTW I use my own axes from Input $$anonymous$$anager, so the code will not work without them.
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?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612233335im_/https://answers.unity.com/themes/thub/images/avi.jpg)