I have two rotations, the objects current rotation, and the target objects rotation. I would like to create a single float that increases as the two rotations differ maxing out at 180° (facing opposite directions). I spent a long time looking into options and found “ToAngleAxis”. This lets me grab a single number for the rotations and use that to find the difference.
Bear with me as this gets a little confusing - but the problem is that when the object is facing the opposite direction the displacement is 180° - but if it continues to rotate towards the target rotation it goes all the way to 360° instead of back down to zero.
Anyone have any ideas on how to get the displacement to max out at 180° and go back down to zero as it turns in a full circle?
Here’s my code that will work with two cubes if you want to test out my problem:
public Transform target;
float curAngle;
Vector3 curAxis;
float targAngle;
Vector3 targAxis;
void Update () {
transform.rotation.ToAngleAxis(out curAngle, out curAxis);
target.rotation.ToAngleAxis(out targAngle, out targAxis);
double displacement = 1-(curAngle-targAngle);
Debug.Log(displacement);
}
Aren’t rotations FUN?!??!?
In case anyone is wondering WHY I’m trying to do this – I’d love to sync the pitch of an audio source to how closely it’s looking towards the player.