I am trying to rotate my car sprite just slightly to the left or right depending on input up to a small maximum. When I use this code, the rotation doesn't stop at the maximum i give it. When i try to reset the rotation back to its original state when not pressing any input, the car also jitters between a z axis rotation of 0 and 2.5. I don't understand why the rotation doesn't stop at the given maximums, nor do I understand why its jittering. Can anybody provide me some insight?
float movementHorizontal = 0f;
float movementVertical = 0f;
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D))
{
}
else if (Input.GetKey(KeyCode.A))
{
movementHorizontal = -1;
if (transform.rotation.z < 10f)
{
transform.Rotate(zAxis, 2.5f);
}
}
else if (Input.GetKey(KeyCode.D))
{
movementHorizontal = 1;
if (transform.rotation.z > -10f)
{
transform.Rotate(zAxis, -2.5f);
}
}
else
{
if(transform.rotation.z > 0)
{
transform.Rotate(zAxis, -2.5f);
//transform.rotation.Set(0, 0, 0, 0);
}
else if (transform.rotation.z < 0)
{
transform.Rotate(zAxis, 2.5f);
//transform.rotation.Set(0, 0, 0, 0);
}
}
↧