# Calculate Difference Between Two 3d Angles

I have a rigidbody and a point in space, and I’m trying to use `Rigidbody.AddTorque` to make the rigidbody attract to the position and rotation of the point. Here’s the code I have so far;
public Rigidbody ObjectToAttract;
[Range(0, 50)]
public float Agressiveness = 10;
[Range(0, 10)]
public float RotationalAgressiveness = 10;

``````    void FixedUpdate()
{
Vector3 Difference = transform.position - ObjectToAttract.transform.position;
ObjectToAttract.AddForce(Difference * Agressiveness - ObjectToAttract.velocity, ForceMode.VelocityChange);
Vector3 AngDifference = AngleDifference(ObjectToAttract.transform.rotation.eulerAngles, transform.rotation.eulerAngles);
ObjectToAttract.AddTorque(AngDifference * RotationalAgressiveness - ObjectToAttract.angularVelocity, ForceMode.VelocityChange); // This line causes issues
}
``````

And here’s the `AngleDifference` function;

``````    private Vector3 AngleDifference(Vector3 Angle1, Vector3 Angle2)
{
Vector3 Difference = new Vector3();
Difference.x = Angle2.x - Angle1.x;
if (Difference.x > 180) Difference.x -= 360;
else if (Difference.x < -180) Difference.x += 360;
Difference.y = Angle2.y - Angle1.y;
if (Difference.y > 180) Difference.y -= 360;
else if (Difference.y < -180) Difference.y += 360;
Difference.z = Angle2.z - Angle1.z;
if (Difference.z > 180) Difference.z -= 360;
else if (Difference.z < -180) Difference.z += 360;
return Difference;
}
``````

I have the code for attracting the rigidbody to a specific position working fine, but the code for applying torque to it causes it to spin in seemingly random directions, regardless of the angle I rotate the point to. What am I doing wrong here?

While MSpiteri is correct about the way to find the angle between two vectors, from you code it looks like you are trying to find the angle between two QUATERNIONS. The Vector3.Angle function is NOT going to work right on “euler angles”. Instead, use the Quaternion.Angle function.

e.g.

``````float AngleDiff =  Quaternion.Angle(ObjectToAttract.transform.rotation, transform.rotation);
``````

However, I don’t think a single float angle is going to be what you want. I think you will want a Quaternion result, that represents the DIFFERENCE in rotations, AS a rotation.

The quaternion MULTIPLY function is used to combine two rotations A and B into a final rotation C. You can also multiply by negative rotations, to get the difference in rotations.

Just a note. If you have 2 vectors and you want to find the angle between them, you can use Vector3.Angle

Once you know the angle, you can apply torque forces to make the object look in the same direction as the target point.

Hope this helps.