Vector3
Vector3 represents three-dimensional space as coordinates, physical quantities, or directions, containing three numerical components (x, y, and z). It is used in Unity for physics, animations, and transformations.
Properties
Instantiates a new Vector3 object by specifying x, y, and z coordinates, representing a point in the Unity game world's 3D space.
Returns a version of this Vector3 with a magnitude of one, maintaining its direction. Useful for unit direction vectors.
local normalizedVector = position.normalized
print(normalizedVector) -- Vector3(0.26726123690605, 0.5345224738121, 0.80178368043899)
Provides the length of a Vector3, calculated as the Euclidean distance to (0,0,0), useful for distance calculations in 3D space.
local magnitude = position.magnitude
print(magnitude) -- 3.7416573867739
Returns the square of the magnitude, an alternative for performance-sensitive calculations where exact distance is not necessary.
local sqrMagnitude = position.sqrMagnitude
print(sqrMagnitude) -- 14
Represents the x-coordinate of the Vector3, allowing direct manipulation or reading of its x-component.
position.x = 5
print(position.x) -- 5
Corresponds to the y-coordinate of the Vector3, for manipulating or reading the y-component.
position.y = 10
print(position.y) -- 10
Represents the z-coordinate of the Vector3, used for direct manipulation or reading of the z-component.
position.z = 15
print(position.z) -- 15
Generates a Vector3 with zero values for all coordinates, representing the origin in 3D space or indicating no motion or rotation.
local zeroVector = Vector3.zero
print(zeroVector) -- Vector3(0, 0, 0)
Generates a Vector3 object with all components set to 1, useful for pointing diagonally in 3D space.
local oneVector = Vector3.one
print(oneVector) -- Vector3(1, 1, 1)
Returns a Vector3 pointing towards the positive Z-axis, useful for designating a forward direction.
Provides a Vector3 pointing towards the negative Z-axis, often used to designate a backward direction.
local backVector = Vector3.back
print(backVector) -- Vector3(0, 0, -1)
Offers a Vector3 pointing upwards along the positive Y-axis, handy for upward movements or orientations.
local upVector = Vector3.up
print(upVector) -- Vector3(0, 1, 0)
Represents a Vector3 pointing downwards along the negative Y-axis, typically used for downward directions.
local downVector = Vector3.down
print(downVector) -- Vector3(0, -1, 0)
Returns a Vector3 pointing in the negative X-axis direction, used to signify left direction in 3D space.
local leftVector = Vector3.left
print(leftVector) -- Vector3(-1, 0, 0)
Provides a Vector3 pointing towards the positive X-axis, recognized as the right direction in 3D space.
local rightVector = Vector3.right
print(rightVector) -- Vector3(1, 0, 0)
Yields a Vector3 with all components set to positive infinity, useful for representing very large values.
local positiveInfinityVector = Vector3.positiveInfinity
print(positiveInfinityVector) -- Vector3(inf, inf, inf)
Generates a Vector3 with all components set to negative infinity, representing the smallest possible values in 3D space.
local negativeInfinityVector = Vector3.negativeInfinity
print(negativeInfinityVector) -- Vector3(-inf, -inf, -inf)
Provides a very small positive number, used for comparisons to determine if Vector3 components are approximately zero, considering floating-point inaccuracies.
local epsilon = Vector3.kEpsilon
print(epsilon) -- 1.4012984643248e-45
A small positive number used in square root calculations, representing a negligible threshold value for comparing Vector3 magnitudes.
local epsilonSqrt = Vector3.kEpsilonNormalSqrt
print(epsilonSqrt) -- 1.1754943508223e-38
Methods
Changes the x, y, and z values of an existing Vector3 object, useful for simultaneous updates to all three components.
position:Set(5, 10, 15)
print(position) -- Vector3(5, 10, 15)
Parameters
newX
The new value for the x-coordinate.
newY
The new value for the y-coordinate.
newZ
The new value for the z-coordinate.
Returns
Multiplies each component of the Vector3 by the corresponding components of another Vector3, used to alter the scale without changing its direction.
position:Scale(Vector3.new(2, 2, 2))
print(position) -- Vector3(10, 20, 30)
Parameters
scale
Vector3A Vector3 by which the instance vector is scaled.
Returns
Adjusts the Vector3 to a length of one while maintaining its direction, useful for when only the direction is of interest.
position:Normalize()
print(position) -- Vector3(0.26726123690605, 0.5345224738121, 0.80178368043899)
Returns
Creates a smooth transition between two Vector3 points on a sphere's surface, ideal for smooth transitions in rotations or positions.
local start = Vector3.new(1, 0, 0)
local finish = Vector3.new(0, 1, 0)
local slerpVector = Vector3.Slerp(start, finish, 0.5)
print(slerpVector) -- Vector3(0.70710676908493, 0.70710676908493, 0)
Similar to 'Slerp' but allows the 't' value to exceed the 0 to 1 range, enabling extrapolation beyond the original points.
local position = Vector3.new(1, 0, 0)
local a = Vector3.new(0, 1, 0)
local b = Vector3.new(0, 0, 1)
local t = 1.5
local result = Vector3.SlerpUnclamped(a, b, t)
print(result) -- Vector3(0, -0.5, 0.5)
Gradually rotates a Vector3 'current' to align with a 'target' Vector3, with specified maximum rotation and magnitude change limits.
local current = Vector3.new(1, 0, 0)
local target = Vector3.new(0, 1, 0)
local maxRadiansDelta = 0.1
local maxMagnitudeDelta = 0.1
local result = Vector3.RotateTowards(current, target, maxRadiansDelta, maxMagnitudeDelta)
print(result) -- Vector3(0.70710676908493, 0.70710676908493, 0)
Performs linear interpolation between two Vector3s, transitioning from 'a' to 'b' based on the 't' ratio.
local start = Vector3.new(1, 0, 0)
local finish = Vector3.new(0, 1, 0)
local lerpVector = Vector3.Lerp(start, finish, 0.5)
print(lerpVector) -- Vector3(0.5, 0.5, 0)
Similar to 'Lerp', this method performs linear interpolation between two points, but allows 't' to exceed the 0 to 1 range, enabling extrapolation.
local start = Vector3.new(1, 0, 0)
local finish = Vector3.new(0, 1, 0)
local t = 1.5
local result = Vector3.LerpUnclamped(start, finish, t)
print(result) -- Vector3(-0.5, 0.5, 0)
Moves a Vector3 ('current') incrementally towards a target Vector3 ('target') at a specified 'maxDistanceDelta'.
local current = Vector3.new(1, 0, 0)
local target = Vector3.new(0, 1, 0)
local maxDistanceDelta = 0.1
local result = Vector3.MoveTowards(current, target, maxDistanceDelta)
print(result) -- Vector3(0.9, 0.1, 0)
Creates a new Vector3 from the product of corresponding components of two given Vector3s, affecting the size without changing direction.
local a = Vector3.new(1, 2, 3)
local b = Vector3.new(4, 5, 6)
local result = Vector3.Scale(a, b)
print(result) -- Vector3(4, 10, 18)
Calculates the cross product of two Vector3 instances, resulting in a Vector3 perpendicular to both, used for finding normals or rotation axes.
local lhs = Vector3.new(1, 0, 0)
local rhs = Vector3.new(0, 1, 0)
local crossProduct = Vector3.Cross(lhs, rhs)
print(crossProduct) -- Vector3(0, 0, 1)
Reflects a Vector3 off a surface with a specified normal, useful for simulating reflections or ricochets.
local inDirection = Vector3.new(1, 0, 0)
local inNormal = Vector3.new(0, 1, 0)
local reflectedVector = Vector3.Reflect(inDirection, inNormal)
print(reflectedVector) -- Vector3(1, 0, 0)
Normalizes a Vector3, returning a new Vector3 with the same direction but a magnitude of 1, useful for direction without magnitude concern.
local vector = Vector3.new(1, 2, 3)
local normalizedVector = Vector3.Normalize(vector)
print(normalizedVector) -- Vector3(0.26726123690605, 0.5345224738121, 0.80178368043899)
Calculates the Dot Product of two Vector3s, useful for determining directional alignment or the angle between vectors.
local lhs = Vector3.new(1, 0, 0)
local rhs = Vector3.new(0, 1, 0)
local dotProduct = Vector3.Dot(lhs, rhs)
print(dotProduct) -- 0
Projects a Vector3 onto another, useful for finding the component of one vector in the direction of another.
local vector = Vector3.new(1, 2, 3)
local onNormal = Vector3.new(1, 0, 0)
local projectedVector = Vector3.Project(vector, onNormal)
print(projectedVector) -- Vector3(1, 0, 0)
Projects a Vector3 onto a plane defined by a normal orthogonal Vector3, useful for aligning objects to surfaces.
local vector = Vector3.new(1, 2, 3)
local planeNormal = Vector3.new(0, 1, 0)
local projectedVector = Vector3.ProjectOnPlane(vector, planeNormal)
print(projectedVector) -- Vector3(1, 0, 3)
Calculates the angle in degrees between two Vector3s, useful for determining the required rotation to align one Vector3 with another.
local from = Vector3.new(1, 0, 0)
local to = Vector3.new(0, 1, 0)
local angle = Vector3.Angle(from, to)
print(angle) -- 90
Calculates the angle between two Vector3s with a sign indicating rotation direction around a given axis.
local from = Vector3.new(1, 0, 0)
local to = Vector3.new(0, 1, 0)
local axis = Vector3.new(0, 0, 1)
local angle = Vector3.SignedAngle(from, to, axis)
print(angle) -- 90
Calculates the distance between two Vector3 points, typically used to determine how far apart two objects are in 3D space.
local a = Vector3.new(1, 0, 0)
local b = Vector3.new(0, 1, 0)
local distance = Vector3.Distance(a, b)
print(distance) -- 1.4142135381699
Limits the magnitude of a Vector3 to a specified maximum, useful for constraining movement or other vector magnitudes.
local vector = Vector3.new(1, 2, 3)
local maxLength = 2
local clampedVector = Vector3.ClampMagnitude(vector, maxLength)
print(clampedVector) -- Vector3(0.55470019578934, 1.1094003915787, 1.6641006469727)
Returns the magnitude (length) of a Vector3, useful for distance calculations from the origin.
local vector = Vector3.new(1, 2, 3)
local magnitude = Vector3.Magnitude(vector)
print(magnitude) -- 3.7416573867739
Parameters
vector
Vector3The Vector3 of which to calculate the magnitude.
Returns
The magnitude of the Vector3.
Calculates the square of the magnitude of a Vector3, often used for performance reasons in relative length comparisons.
local vector = Vector3.new(1, 2, 3)
local sqrMagnitude = Vector3.SqrMagnitude(vector)
print(sqrMagnitude) -- 14
Parameters
vector
Vector3The Vector3 of which to calculate the square of the magnitude.
Returns
The square of the magnitude of the Vector3.
Returns a Vector3 composed of the smallest x, y, and z components from two given Vector3s, useful for bounding calculations.
local a = Vector3.new(1, 2, 3)
local b = Vector3.new(3, 2, 1)
local minVector = Vector3.Min(a, b)
print(minVector) -- Vector3(1, 2, 1)
Produces a Vector3 from the largest x, y, and z components of two given Vector3s, useful for defining bounds or extents.
local a = Vector3.new(1, 2, 3)
local b = Vector3.new(3, 2, 1)
local maxVector = Vector3.Max(a, b)
print(maxVector) -- Vector3(3, 2, 3)