Unity Vector2

  

Unity Converters for Newtonsoft.Json. This package contains converters to and from common Unity types. Types such as Vector2, Vector3, Matrix4x4, Quaternions, Color, even ScriptableObject, and many, many more. (See the full compatibility table of all +50 supported Unity types). The perfect complement to the jilleJr/Newtonsoft.Json-for-Unity repo. ⭐ JUST BEEN RELEASED! Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.

  1. Unity Vector2 Api
  2. Unity Vector2 Magnitude
Vector2

This function allows you to calculate the angle between two vectors without having to deal with Trigonometric functions, but it has a downside. The way it works is not as clear as one could wish.

Vector2Vector2

I used this function to allow an agent (I.A.) to choose among the exits in a dungeon room. I needed the agent to be able to recognize the exits to his left or right.

I created a list containing the exits positions, I needed a way to sort them out in a clockwise order, here is where Vector2.Angle comes into play. After some debugging I realized that I couldn’t jut use positions to calculate this. Specially considering that whenever an agent enters a room he is facing a specific direction, therefore the exit to its right would depend of where it is facing at that moment.

I created a scene where I could test this, here are some screenshots in which you can see that the agent at the centre is facing a specific direction (in this case I restricted it to North, South, East or West).

In order to correctly use Vector2.Angle you need to input direction vectors. In my case one of them is the direction the agent is facing and the second one is created from the agent and exit’s position.

Unity Vector2 Api

A vector V that goes from A to B is created by subtracting A from B. V = BA.

The angle returned from Vector2.Angle is a result from the dot product of the vectors therefore it’s values are between 0 and 180.
With the variable “orientation” we find out if it is going clockwise or counter clockwise. With this knowledge at hand we can get the angle value in a 360 degree format.

Unity Vector2 Magnitude

Feel free to download browse the attached project and comment any questions you might have.