- Home /
How to rotate object 180 degrees smoothly
I've tried googling my problem whole day and can't get it to work. The game is a sidescroller. I've got an aeroplane that flies and when it rotates its Z over it's supposed to rotate its X 180 degrees so the pilot is never upside down.
The problem is that it doesn't do it clean and sometimes the plane stops its rotation in the middle of the turning and messes up the directions.
If someone could tell me how to do it so the object rotates 180 degrees smoothly from 0 to 180 degrees it would be awesome.
Here's my code:
if (Input.GetAxis ("Horizontal") < 0 || Input.GetAxis ("Horizontal") > 0) transform.Rotate(0,0,-1*Input.GetAxis ("Horizontal") * 150 * Time.deltaTime,Space.World);
if (transform.rotation.eulerAngles.z > 90 && transform.rotation.eulerAngles.z < 110 && doRotate == 0) doRotate = 1;
if (transform.rotation.eulerAngles.z > 250 && transform.rotation.eulerAngles.z < 270 && doRotate == 0) doRotate = 1;
if (doRotate == 1) transform.Rotate(300*Time.deltaTime,0,0,Space.Self);
if (doRotate == 1 && transform.rotation.eulerAngles.x < 180) {
transform.rotation.eulerAngles.x = 0;
doRotate = 0;
}
Answer by Goodgab · Mar 16, 2013 at 06:24 PM
This is my code for rotating a plane when clicked 180 degrees. When clicked again, it rotates bakc 180 degrees.
var rotateFront = false;
var rotateBack = false;
function Start ()
{
}
function Update ()
{
if (rotateFront)
Reveal();
if (rotateBack)
Hide();
}
function OnMouseDown()
{
if(rotateFront)
{
rotateFront = false;
rotateBack = true;
}
else if (rotateBack)
{
rotateFront = true;
rotateBack = false;
}
else
rotateFront = true;
}
function Reveal()
{
if (transform.rotation.y < 1)
transform.Rotate(0,1,0); //you can change axis to rotate and speed
}
function Hide()
{
if (transform.rotation.y > 0)
transform.Rotate(0,-1,0);
}
the Reveal() and Hide() functions do what you are asking for.
Answer by robertbu · Mar 16, 2013 at 07:28 PM
I can tell you what your problem is, but I cannot give example code without a better description of your situation (which way the camera is facing and a graphic of why the plane needs to be flipped). My guess is there is a solution without doing the flipping.
But as for the problem with the code above, for any given physical rotation of your object, there are multiple euler angle representations. If you rotate 180 degrees about the 'X' axis for example, reading back transform.eulerAngles.x (which you can do instead of transform.rotation.eulerAngles) may not produce a value of 180. Instead you may see a rotation on the z and y axex and a 0 rotation on 'x' axis (which would be the same physical rotation).
A solution I use is to keep my own Vector3 and set (but never read) the euler angles. Note you should always set all three euler angles together. Never set them independently.
So the code might look like:
angle += 300 * Time.deltaTime;
transform.eulerAngles = Vector3(angle, 0.0, 0.0);
Your answer
![](https://koobas.hobune.stream/wayback/20220613084837im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Rotation Lerp Back To 0 Degrees, Please Help 2 Answers
Problem with oscillation 3 Answers
0-360 Y degree from Vector3.Angle 1 Answer
Move an object toward an angle in 2d space 0 Answers
Detecting the range between 2 angles 2 Answers