I am making a little 2D RTS were i often need to know the angle between a units current rotation and a “target” rotation. So far Quaternion.Angle has been sufficient but now i need to know weather the relative angle is clockwise or anticlockwise as well. I have tried using eulerAngles and ToAngleAxis but both yield inconsistent results while the unit is in motion for reasons i don’t fully understand but seem to be normal from what my googling has turned up.
So yeah. Any idea how i do this? Thanks in advance, I’m happy to try and clarify anywhere I’ve been unclear.
Sorry, I’m a bit tired right now and your question doesn’t seem to make much sense. Can you explain what you’re trying to do with the angles?
I have two rotations and i want to know the relative angle between them and weather the difference is in the clockwise or anticlockwise direction (it’s a 2d game with all rotation z axis). I hope that helps? I’m very new to this and don’t have any sort of grip on the appropriate language.
I think you can just do this?
float angleA = transform.rotation.eulerAngles.z;
float angleB = OtherObject.rotation.eulerAngles.z;
float d = Mathf.DeltaAngle(angleA, angleB);
if (d > 0.0f)
{
Debug.Log("Clockwise");
}
else if (d < 0.0f)
{
Debug.Log("Counter Clockwise");
}
Thanks. I don’t know why eulerAngles wasn’t working before but it’s working now.
The previous reply wont work the way you want it to. This seems to be working for me. You might need to adjust the axis.
Vector3 direction = targetObject.position - transform.position;
float currentAngle = Mathf.Atan2(transform.forward.y, transform.forward.x) * Mathf.Rad2Deg;
float targetAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
float deltaAngle = Mathf.DeltaAngle(currentAngle, targetAngle);
if (deltaAngle < 0.0f)
{
//Clockwise
}
if (deltaAngle > 0.0f)
{
//Counter Clockwise
}
1 Like