CraftStudio Wiki
CraftStudio Wiki

Vector3 properties[]

A Vector3 is an object holding a math vector (which can represent a position, a direction or an offset for instance).

Vector3s contain 3 properties x, y, z.

Example: Printing the Y component of a vector in the runtime report[]

​local position = self.gameObject.transform:GetPosition()
print( "Y is: " .. position.y )

Vector3:New

[Vector3] Vector3:New( [number] x, [number] y, [number] z )
[Vector3] Vector3:New( [number] xyz )

Returns a new Vector3.

Vector3:Length, Vector3:SqrLength[]

[number] Vector3:Length()
[number] Vector3:SqrLength()

Returns the vector's (actual or squared) magnitude / length.

Example: Trimming a vector to a specific length[]

local someVector = Vector3:New( 50, 20, 30 )
local maxLength = 10

if someVector:Length() > maxLength then
    someVector = someVector:Normalized() * maxLength
end

Vector3:Normalize[]

Vector3:Normalize()

Normalize the vector

Vector3:Normalized[]

[Vector3] Vector3:Normalized()

Returns a copy of the vector, normalized

Vector3:Add[]

Vector3:Add( [Vector3] v )

Adds the specified vector

Vector3:Subtract[]

Vector3:Subtract( [Vector3] v )

Subtract the specified vector

Vector3.Lerp[]

[Vector3] Vector3.Lerp( [Vector3] a, [Vector3] b, [number] amount )

Return the vector resulting from the linear interpolation between vector a and b by the specified amount.

Vector3.Slerp[]

[Vector3] ​Vector3.Slerp( [Vector3] a, [Vector3] b, [number] amount )

Return the vector resulting from the spherical linear interpolation between vector a and b by the specified amount.

Vector3.Dot[]

Vector3.Cross[]

Vector3.Angle[]

​[number] Vector3.Angle( [Vector3] a, [Vector3] b )

Vector3.Distance[]

[Number] Vector3.Distance( [Vector3] v1, [Vector3] v2 )

Returns the Distance between v1 and v2

Vector3.Transform[]

[Vector3] Vector3.Transform( [Vector3] v, [Quaternion] rotation )

Returns a new version, result of the transformation of the specified vector by the specified rotation

Vector3 * number[]

Returns a new vector whose components are equal to those specified vector multiplied by the specified number

Vector3 * Vector3[]

Returns a new vector whose components are equal to each component of the first vector multiplied by each component of the second vector

Vector3 / Vector3[]

Returns a new vector whose components are equal to each component of the first vector divided by each component of the second vector

Vector3 + Vector3[]

Returns a new vector whose components are equal to each component of the second vector added to those of the first vector

Vector3 - Vector3[]

Returns a new vector whose components are equal to each component of the second vector subtracted from those of the first vector

Vector3:Left, Vector3:Up, Vector3:Forward[]

[Vector3] Vector3:Left()
[Vector3] Vector3:Up()
[Vector3] Vector3:Forward()

Returns a unit vector pointing left (X=-1, Y=0, Z=0), up (X=0, Y=1, Z=0) or forward (X=0, Y=0, Z=-1)

Example: Getting a vector pointing forward for a particular game object[]

-- Start with the absolute forward direction
local direction = Vector3:Forward()​

-- Rotate it to match the object's current orientation
direction = Vector3.Transform( direction, self.gameObject.transform:GetOrientation() ) -- relative forward direction

Vector3:UnitX, Vector3:UnitY, Vector3:UnitZ[]

[Vector3] Vector3:UnitX()
[Vector3] Vector3:UnitY()
[Vector3] Vector3:UnitZ()

Returns a unit vector pointing on the X, Y or Z axis.