Vector3

Introduction

The Vector3 structure represents a 3D coordinate, and is one of the backbone structures of the UnityEngine library. The Vector3 structure is most commonly found in the Transform component of most game objects, where it is used to hold position and scale. Vector3 provides good functionality for performing common vector operations. You can read more on the Vector3 structure in the Unity API.

Syntax

• public Vector3();
• public Vector3(float x, float y);
• public Vector3(float x, float y, float z);
• Vector3.Lerp(Vector3 startPosition, Vector3 targetPosition, float movementFraction);
• Vector3.LerpUnclamped(Vector3 startPosition, Vector3 targetPosition, float movementFraction);
• Vector3.MoveTowards(Vector3 startPosition, Vector3 targetPosition, float distance);

Applying Movement

The Vector3 structure contains some static functions that can provide utility when we wish to apply movement to the Vector3.

Lerp and LerpUnclamped

The lerp functions provide movement between two co-ordinates based off a provided fraction. Where Lerp will only permit movement between the two co-ordinates, LerpUnclamped allows for fractions that move outside of the boundaries between the two co-ordinates.

We provide the fraction of movement as a float. With a value of 0.5, we find the midpoint between the two Vector3 co-ordinates. A value of 0 or 1 will return the first or second Vector3, respectivley, as these values either correlate to no movement (thus returning the first Vector3), or completed movement (this returning the second Vector3). It is important to note that neither function will accommodate for change in the movement fraction. This is something we need to manually account for.

With Lerp, all values are clamped between 0 and 1. This is useful when we want to provide movement towards a direction, and do not want to overshoot the destination. LerpUnclamped can take any value, and can be used to provide movement away from the destination, or past the destination.

The following script uses Lerp and LerpUnclamped to move an object at a consistent pace. MoveTowards

MoveTowards behaves very similar to Lerp; the core difference is that we provide an actual distance to move, instead of a fraction between two points. It is important to note that MoveTowards will not extend past the target Vector3.

Much like with LerpUnclamped, we can provide a negative distance value to move away from the target Vector3. In such cases, we never move past the target Vector3, and thus movement is indefinite. In these cases, we can treat the target Vector3 as an "opposite direction"; as long as the Vector3 points in the same direction, relative to the start Vector3, negative movement should behave as normal.

The following script uses MoveTowards to move a group of objects towards a set of positions using a smoothed distance. SmoothDamp

Think of SmoothDamp as a variant of MoveTowards with built in smoothing. According to official documentation, this function is most commonly used to perform smooth camera following.

Along with the start and target Vector3 coordinates, we must also provide a Vector3 to represent the velocity, and a float representing the approximate time it should take to complete the movement. Unlike previous examples, we provide the velocity as a reference, to be incremented, internally. It is important to take note of this, as changing velocity outside of the function while we are still performing the function can have undesired results.

In addition to the required variables, we may also provide a float to represent the maximum speed of our object, and a float to represent the time gap since the previous SmoothDamp call to the object. We do not need to provide these values; by default, there will be no maximum speed, and the time gap will be interpretted as Time.deltaTime. More importantly, if you are calling the function one per object inside a MonoBehaviour.Update() function, you should not need to declare a time gap. Creating a Vector3

A Vector3 structure can be created in several ways. Vector3 is a struct, and as such, will typically need to be instantiated before use.

Constructors

There are three built in constructors for instantiating a Vector3.

ConstructorResult
new Vector3()Creates a Vector3 structure with co-ordinates of (0, 0, 0).
new Vector3(float x, float y)Creates a Vector3 structure with the given x and y co-ordinates. z will be set to 0.
new Vector3(float x, float y, float z)Creates a Vector3 structure with the given x, y and z co-ordinates.

Converting from a Vector2 or Vector4

While rare, you may run into situations where you would need to treat the co-ordinates of a Vector2 or Vector4 structure as a Vector3. In such cases, you can simply pass the Vector2 or Vector4 directly into the Vector3, without previously instantiating it. As should be assumed, a Vector2 struct will only pass x and y values, while a Vector4 class will omit its w.

We can see direct conversion in the below script. Static Values

The Vector3 structure contains some static variables that provide commonly used Vector3 values. Most represent a direction, but they can still be used creatively to provide additional functionality.

Vector3.zero and Vector3.one

Vector3.zero and Vector3.one are typically used in connection to a normalised Vector3; that is, a Vector3 where the x, y and z values have a magnitude of 1. As such, Vector3.zero represents the lowest value, whilst Vector3.one represents the largest value.

Vector3.zero is also commonly used to set the default position on object transforms.

The following class uses Vector3.zero and Vector3.one to inflate and deflate a sphere. Static Directions

The static directions can be useful in a number of applications, with direction along the positive and negative of all three axis. It is important to note that Unity employs a left-handed coordinate system, which has an affect on direction. The following class uses the static Vector3 directions to move objects along the three axis. Index

ValuexyzEquivalent new Vector3() method
Vector3.zero000new Vector3(0, 0, 0)
Vector3.one111new Vector3(1, 1, 1)
Vector3.left-100new Vector3(-1, 0, 0)
Vector3.right100new Vector3(1, 0, 0)
Vector3.down0-10new Vector3(0, -1, 0)
Vector3.up010new Vector3(0, 1, 0)
Vector3.back00-1new Vector3(0, 0, -1)
Vector3.forward001new Vector3(0, 0, 1)