Unity Vector3 tips and tricks

A vector is something that has direction and magnitude. In Unity, vectors are divided based on dimensions. A three Dimensional vector is called as Vector3. A vector3 contains the magnitude and direction information for all 3 dimensions. In game development Vector 3 is mostly use to find the position of an object and distance between object. In this tutorial, we will see how to use Vector3 to it maximum potential.

syntax for Vector3

A Vector3 in Unity is represented by the name Vector3 followed by the magnitude along each axis.

Vector3(1.0f,2.0f,-6.2f);

By default a Vector3 takes float as input. If you do not require the precision of float then you can specify the input as integer in the manner shown below.

Vector3int(1,2,6);

Declaring a new Vector3

Vector3 testvector=new Vector3(0,1,1);

Shortforms of Vector3

VectorShortform
Vector3(0, 0, -1)Vector3.back
Vector3(0, -1, 0) Vector3.down
Vector3(0, 0, 1) Vector3.forward
Vector3(-1, 0, 0) Vector3.left
Vector3(1, 1, 1) Vector3.one
Vector3(1, 0, 0) Vector3.right
Vector3(0, 1, 0) Vector3.up
Vector3(0, 0, 0) Vector3.zero

Useful methods and Properties of Vector3

Method/property syntaxWhat it does
Vector3.magnitudeReturns the length of the vector.
Vector3.normalizedReturns the length of the vector with a magnitude of 1.
Vector3.normalizeChange the length of the vector to a magnitude of 1.
Float xvalue = Vector3.x(same for y and z)Returns the x component of the Vector
Vector3.x = xvalue (same for y and z)Write the value into the x component of the Vector
Vector3.ClampMagnitude(YourVector,clamp distance)Clamps the magnitude of the vector to the specified distance.
Vector3.Distance(First vector, second vector)Returns the distance between two vectors.
Vector3.Lerp(First vector, second vector, any value between 0 &1)Returns a point between the two vectors based on the third input.
Vector3.Movetowards(your position, target, max distance)Move towards the target position
Vector3.RotateTowards(Current direction, targetDirection, singleStep, max magnitude)Rotate towards the target direction

Code samples for common examples.

1.Position of the Gameobject to which the script is attached.

Vector3 Position= transform.position;

2. Position of any Gameobject

Vector3 Position= GameObject.Find("Your Gameobject name").transform.position;

3. Move Gameobject by one unity using transform

transform.position += vector3.one;

4. Slowly move towards a point in steps of 1.

Vector3 target= new vector3(1f,2f,3f);
Void Update()
{
transform.position= Vector3.Movetowards(transform.position, target, 1.0f);

}

5. Slowly Look towards an Enemy

Void Start()
{
Vector3 Enemy_position= GameObject.Find("Enemy").transform.position;

}
Void Update()
{
Vector3 newDir = Vector3.RotateTowards(transform.forward, Enemy_position, 2*Time.deltaTime, 0.0f);
transform.rotation = Quaternion.LookRotation(newDir);
}

If you need any other code sample, leave it in the comment box below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.