We need to find the angle between the vectors. I think that there should be a function or method for such things, but I cannot find it in the reference book.

An example of use is desirable.

  • Find the scalar product of vectors, and recall the well-known formula: a•b = |a|•|b|•cos alpha ; Well, and then take the arc cosine. - Yaant

1 answer 1

There is a standard Vector3.Angle method - which returns the smallest angle between two vectors described in the unit documentation https://docs.unity3d.com/ScriptReference/Vector3.Angle.html

It is relatively easy to use.

 Vector3 targetDirection = target.transform.position - transform.position; angleBetween = Vector3.Angle(transform.forward, targetDirection); 

Voila, we got the smallest angle between our object and the target object. Appropriately, this will also work for d2 coordinates (just z = 0) or if it’s not convenient for you to use (absolutely) Vector2.Angle https: //docs.unity3d .com / ScriptReference / Vector2.Angle.html

  • I have already figured out, but the explanations of the method are incorrect. Example too. - Mr. Fix
  • Well, you know better :) - astion