- Home /
Problems with code to tilt an object left and right
Hi!
I've recently started using Unity and c#, although I'm primarily a Gamemaker dev. I moved over here since it doesn't have great 3D support.
I've started off by making a Voxel style shmup. It's going fairly well, but I'm having trouble with making the ship tilt left and right.
float moveHorizontal = Input.GetAxis("Horizontal");
if (moveHorizontal > 0) //if you press the right arrow, tilt right
{
transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0, 0, 0), Quaternion.Euler(0, 0, -30), 50 * rotatespeed);
}
else //else, go back to the default rotation
{
transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0, 0, 0), Quaternion.Euler(0, 0, 0), 50 * rotatespeed);
}
if (moveHorizontal < 0)//if you press left, tilt left
{
transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0, 0, 0), Quaternion.Euler(0, 0, 30), 50 * rotatespeed);
}
else//else, go back to the default rotation again
{
transform.rotation = Quaternion.RotateTowards(Quaternion.Euler(0, 0, 0), Quaternion.Euler(0, 0, 0), 50 * rotatespeed);
}
Now I know there's probably better ways to handle this, if there are, let me know.
The problems I am having is that the ship seems to only tilt left, and the rotation is instant, even though I was told that Quaternion.RotateTowards would provide a smooth rotation. Does anyone have any ideas on how I could fix this, or any alternatives to this code?
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How to lock z axis rotation 2 Answers
How can I have 2 transforms have the same position and rotation 0 Answers
Distribute terrain in zones 3 Answers