Understanding “Vector” in Unity

Definition:  In unity, Vector is the data type where you can do all types of vector calculation (Dot product, Cross product, Normalize, Projection, Magnitude) and other unity given function such as “Distance” & “MoveTowards” etc. As the “Distance” and “MoveTowards” are highly used in our “Game Development” process. We will try to describe as much as possible down below.

 

  • Distance: It calculate the distance between 2 vector point. We usually use it for detection or creating a sensor like a game object will react when the user comes to a certain point.

Parameter:         Vector2.Distance(Vector2.position, Vector2.position)

Vector3.Distance(Vector3.position, Vector3.position).

 

Vector (1)

Here, the “Mine” will be explode when the distance between the “Player” & “Mine” will be less than or equal to “3.14”. As the “Player” is at “5.14” distance, so the “Mine” is not exploding. We can see the following script for more detail.


  • MoveTowards: It basically moves the current position of the game object to the targeted position based on the velocity that has been assigned in the “MoveTowards” function parameter.

Parameter:

Vector2.MoveTowards(Vector2.position (Current), Vector2.position (Target), Velocity).

Vector3.MoveTowards(Vector3.position (Current), Vector3.position (Target), Velocity).

               

Problem: Let us consider that we have a homing missile that chases the player. If the player is within its range, It will start chasing the player. Let’s see the following script reference.

 

 


# The reference of the documents are taken from the “Unity – Documentation”.

Muhammad Tashfiqul Bari

Mentor at Coursera

Author Muhammad Tashfiqul Bari

More posts by Muhammad Tashfiqul Bari

Leave a Reply