Cannot rotate exactly 90 degrees
if (Input.GetKeyDown(KeyCode.W))
{
var Move = transform.position;
Move.y += 1;
transform.position = Move;
}
if (Input.GetKeyDown(KeyCode.S))
{
var Move = transform.position;
Move.y -= 1;
transform.position = Move;
}
if (Input.GetKeyDown(KeyCode.A))
{
transform.Rotate(transform.rotation.x, transform.rotation.y, transform.rotation.z + 90);
}
if (Input.GetKeyDown(KeyCode.D))
{
transform.Rotate(transform.rotation.x, transform.rotation.y, transform.rotation.z - 90);
}
For a game, I am working on I want the player to just jump to the next tile which works fine. but I cannot get the player to rotate left or right exactly 90 degrees.
The current A and D keys rotate the player -90.00001 according to the editor.
how can I get the player to rotate exactly 90 degrees?
(also side note if anyone knows how to get the player to move forward according to tp the new rotation that would also be a massive hand thank you!)
Answer by streeetwalker · Sep 13, 2020 at 06:48 AM
@LordBitcube, for all purposes 90.00001 is 90 degrees - there is no difference. What often shown in the inspector is an approximation due to errors in floating point math used by Unity (and all systems have the same non-problem)
If you are displaying a value to your users, then truncate the text displayed.
In sum, floating point operations have inherent inaccuracies. In your case the differences are so small that the are not relevant. It would only be a problem in cases where they accumulate.
It would only be a problem in cases where they accumulate.
They do accumulate is the issue though. the character is supposed to rotate freely in 90-degree increments. is there a way I can achieve this?
You might have better luck with setting the Rotation using Quaternions - you'd have to try it:
transform.rotation = transform.rotation * Quaternion.AngleAxis( 90f, transform.forward );
Works perfectly! thank you for all the help!