- Home /
Rotating an object to see everything correctly
I am currently making a game about spaceships. For that I want to have the possibility to look around as you want and move in any directions (since there is no gravity this makes sense for me). To not see everything on an angle after a while I implemented a System, that turns the players z-axis to the nearest correct Position (0° / 90° / 180° / 270°) if he is looking along an axis. This works fine if you are looking along x or z axis in world space, however not if you are Looking up or down. It still modifies the z-axis as wanted but it is not necissarily correct. Here is my Code: void CorrectRotation() {
float checkX = transform.localRotation.eulerAngles.x % 90;
float checkY = transform.localRotation.eulerAngles.y % 90;
if(checkX < correctRotation || checkX > 90 - correctRotation)
{
if(checkY < correctRotation || checkY > 90 - correctRotation)
{
//Rotation needs to be corrected
float zVal = transform.localRotation.eulerAngles.z % 90;
if(zVal < 45)
{
transform.Rotate(0f, 0f, -zVal * correctionSpeed * Time.fixedDeltaTime, Space.Self);
}
else
{
transform.Rotate(0f, 0f,(90 - zVal) * correctionSpeed * Time.fixedDeltaTime, Space.Self);
}
}
}
}
So to point out what I want: If I look down and there is a cube, I want that the verticies are parallel to my screen borders. I know the issue, so any ideas on how to get this working will help.
Your answer
Follow this Question
Related Questions
Help with rotating a sprite with mouse. 2 Answers
How to Add Knockback Force Based on What Rotation it Came From 2 Answers
My enemy character is not facing the player correctly! 0 Answers
Help regarding "Collect Cubes" Like Player Movement and Rotation 1 Answer
FPS Controller rotation,FPS Controller rotation problem 0 Answers