- Home /
calculating looking angle between 2 transforms
I have 2 transforms or Vector3 and a Quaternion, I want to return the total angle between the two transforms:
if 2 transforms are facing each other it would return 0.
if transform a is facing b but transform b is rotated 20 to the side would return 20.
if transform a is facing b and b is facing in the same direction i would return 180.
if both transforms are looking away from each other as much as possible it would return 360.
Things I have tried so far:
//TODO calculate angular difference
private float oldCalculateOrientationAngle(Vector3 positionA, Quaternion rotationA, Vector3 positionB, Quaternion rotationB)
{
//calculate the rotation from position a to position b
Quaternion positionalQuaternionAB = Quaternion.FromToRotation(positionA, positionB);
float angleA = Quaternion.Angle(rotationA, positionalQuaternionAB);
//calculate the rotation from postion b to position a
Quaternion positionalQuaternionBA = Quaternion.FromToRotation(positionB, positionA);
float angleB = Quaternion.Angle(rotationB, positionalQuaternionBA);
//Return total angle offset of positional angle between Transform a and transform b
Quaternion rotateTowards = Quaternion.RotateTowards(rotationA, rotationB, 360);
return angleA + angleB;
}
private float calculateOrientationAngle(Vector3 positionA, Quaternion rotationA, Vector3 positionB, Quaternion rotationB)
{
oldCalculateOrientationAngle(positionA, rotationA, positionB, rotationB);
Vector3 vectorDisAB = positionB - positionA;
Quaternion idealRotationA = Quaternion.LookRotation(vectorDisAB);
Vector3 vectorDisBA = positionA - positionB;
Quaternion idealRotationB = Quaternion.LookRotation(vectorDisBA);
float angleA = Quaternion.Angle(idealRotationA, rotationA);
float angleB = Quaternion.Angle(idealRotationB, rotationB);
float angleSum = angleA + angleB;
return angleSum;
}
But it doesn't work can someone help.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Making my GambeObject rotate to face the direction of movement, smoothly. 0 Answers
Rotate player (rigidbody) towards his movement 2 Answers
Trouble with Camera Rotation Math 2 Answers
Weird shake when I try to rotate player according to Virtual Cameras rotation. 0 Answers