360 rotation without gimbal lock in Unity

up vote0down votefavorite

I am making 6DOF game (like Descent) with Unity and C#. I have the 6DOF working, but when I rotate to much around the x-as I get the problem with gimbal lock (right become left and left become right).

I use the following code:

float movementSpeed = 50f;
float turnSpeed = 150f;
float arrowMouseSpeed = 1.5f;

void Update()
{
moveWithArrowAndMouse();
Thrust();
}

void moveWithArrowAndMouse()
{
moveCamera(Input.GetAxis(“Mouse X”), Input.GetAxis(“Mouse Y”), arrowMouseSpeed);
}

//Move Parameters
float mouseX;
float mouseY;

Quaternion localRotation;
private float rotY = 0.0f;
private float rotX = 0.0f;
private float roll = 0.0f;

void moveCamera(float horizontal, float verticle, float moveSpeed)
{
mouseX = horizontal;
mouseY = -verticle;

rotY += mouseX * moveSpeed;
rotX += mouseY * moveSpeed;
roll += Input.GetAxis("Roll");

localRotation = Quaternion.Euler(rotX, test * rotY, roll);
transform.rotation = localRotation;

}

void Thrust()
{

transform.position += myT.forward * movementSpeed * Time.deltaTime * Input.GetAxis("Vertical");
transform.position += myT.right * movementSpeed * Time.deltaTime * Input.GetAxis("Horizontal");

}


I Also tried:

void moveCamera(float horizontal, float verticle, float moveSpeed)
{
mouseX = horizontal;
mouseY = -verticle;

rotY += mouseX * moveSpeed;
rotX += mouseY * moveSpeed;
roll += Input.GetAxis("Roll");

Quaternion yaw = Quaternion.AngleAxis(rotX, transform.right);
Quaternion pitch = Quaternion.AngleAxis(rotY, transform.up);
transform.rotation = yaw * pitch;

}


But this gives a flickering screen.

Can anybody change the code above so it's gone working correct?

I moved this topic to general Unity discussion as this doesn’t deal with the book at all.

This topic was automatically closed after 166 days. New replies are no longer allowed.