Vector3

A three-dimensional vector.

# Properties

[{"index":"float x","0":"The x-axis value of the vector."}]

[{"index":"float y","0":"The y-axis value of the vector."}]

[{"index":"float z","0":"The z-axis value of the vector."}]

[{"index":"Vector3 back (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(0, 0, -1)."}]

[{"index":"Vector3 down (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(0, -1, 0)."}]

[{"index":"Vector3 forward (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(0, 0, 1)."}]

[{"index":"Vector3 left (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(-1, 0, 0)."}]

[{"index":"Vector3 negativeInfinity (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(-inf, -inf, -inf)."}]

[{"index":"Vector3 one (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(1, 1, 1)."}]

[{"index":"Vector3 positiveInfinity (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(inf, inf, inf)."}]

[{"index":"Vector3 right (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(1, 0, 0)."}]

[{"index":"Vector3 up (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(0, 1, 0)."}]

[{"index":"Vector3 zero (Static) (ReadOnly) (HideFromInspector)","0":"You can simply use Vector3(0, 0, 0)."}]

# Constructors

[{"index":"Vector3(float x, float y, float z)","0":"Initializes Vector3 to the specified value."}]

# Methods

[{"index":"float Angle(Vector3 b)","0":"Finds the angle between two vectors. Returns a value between 0 and 180."}]

[{"index":"Vector3 Clone()","0":"Generates a copy."}]

[{"index":"Vector3 Cross(Vector3 b)","0":"Creates a Vector3 representing the cross product of two Vectors and returns it."}]

[{"index":"float Distance(Vector3 b)","0":"Finds the distance between two vectors."}]

[{"index":"float Dot(Vector3 b)","0":"Finds the inner product of two vectors."}]

[{"index":"Vector3 Lerp(Vector3 b, float t)","0":"Creates a Vector3 having the value using linear interpolation with t between two Vectors and then returns it. The value of t is between 0 to 1."}]

[{"index":"Vector3 LerpUnclamped(Vector3 b, float t)","0":"Creates a Vector3 entity that linearly interpolates two Vectors with t and then returns it."}]

[{"index":"float Magnitude()","0":"Finds the size of a vector."}]

[{"index":"Vector3 Normalize()","0":"Creates the Vector3 entity with the same direction and size of 1 and then returns it."}]

[{"index":"Vector3 Project(Vector3 b)","0":"Creates a Vector3 entity, which projects Vector with b, and returns it."}]

[{"index":"Vector3 ProjectOnPlane(Vector3 planeNormal)","0":"Creates a Vector3 entity, which projects the Vector to a flat surface, and returns it; planeNormal is the surface's normal Vector."}]

[{"index":"Vector3 Reflect(Vector3 normal)","0":"Creates a Vector3 entity, which projects the Vector to a flat surface, and returns it; normal is the surface's normal Vector."}]

[{"index":"float SignedAngle(Vector3 to, Vector3 axis)","0":"Finds the angle between two Vectors. If the angle relative to from, based on the axis, is counterclockwise, the value is a positive number; if the angle is clockwise, the value is a negative number. Returns values between -180 to 180."}]

[{"index":"Vector3 Slerp(Vector3 b, float t)","0":"Creates a Vector3 entity that spherical-linearly interpolates two Vectors with t and then returns it. The value of t is between 0 to 1."}]

[{"index":"Vector3 SlerpUnclamped(Vector3 b, float t)","0":"Creates a Vector3 entity that spherical-linearly interpolates two Vectors with t and then returns it."}]

[{"index":"float SqrMagnitude()","0":"Finds the square of the size of a Vector."}]

[{"index":"Vector2 ToVector2()","0":"Creates the Vector2 entity with x and y elements, and then returns it."}]

[{"index":"float Angle(Vector3 a, Vector3 b) (Static)","0":"Finds the angle between two vectors. Returns a value between 0 and 180."}]

[{"index":"Vector3 Cross(Vector3 a, Vector3 b) (Static)","0":"Creates a Vector3 representing the cross product of two Vectors and returns it."}]

[{"index":"float Distance(Vector3 a, Vector3 b) (Static)","0":"Finds the distance between two vectors."}]

[{"index":"float Dot(Vector3 a, Vector3 b) (Static)","0":"Finds the inner product of two vectors."}]

[{"index":"Vector3 Lerp(Vector3 a, Vector3 b, float t) (Static)","0":"Creates Vector3 having the value using linear interpolation with t between two Vectors and then returns it. The value of t is between 0 to 1."}]

[{"index":"Vector3 LerpUnclamped(Vector3 a, Vector3 b, float t) (Static)","0":"Creates a Vector3 entity that linearly interpolates two Vectors with t and then returns it."}]

[{"index":"float Magnitude(Vector3 a) (Static)","0":"Finds the size of a vector."}]

[{"index":"Vector3 Normalize(Vector3 a) (Static)","0":"Creates the Vector3 entity with the same direction and size of 1 and then returns it."}]

[{"index":"Vector3 Project(Vector3 a, Vector3 b) (Static)","0":"Creates the Vector3 entity, which projected a with b, and returns it."}]

[{"index":"Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal) (Static)","0":"Creates a Vector3 entity, which projects the Vector to a flat surface, and returns it; planeNormal is the surface's normal Vector."}]

[{"index":"Vector3 Reflect(Vector3 vector, Vector3 normal) (Static)","0":"Creates a Vector3 entity, which projects the Vector to a flat surface, and returns it; normal is the surface's normal Vector."}]

[{"index":"float SignedAngle(Vector3 from, Vector3 to, Vector3 axis) (Static)","0":"Finds the angle between two Vectors. If the angle relative to from, based on the axis, is counterclockwise, the value is a positive number; if the angle is clockwise, the value is a negative number. Returns values between -180 to 180."}]

[{"index":"Vector3 Slerp(Vector3 a, Vector3 b, float t) (Static)","0":"Creates a Vector3 entity that spherical-linearly interpolates two Vectors with t and then returns it. The value of t is between 0 to 1."}]

[{"index":"Vector3 SlerpUnclamped(Vector3 a, Vector3 b, float t) (Static)","0":"Creates a Vector3 entity that spherical-linearly interpolates two Vectors with t and then returns it."}]

[{"index":"float SqrMagnitude(Vector3 a) (Static)","0":"Finds the square of the size of a Vector."}]

[{"index":"Vector2 ToVector2(Vector3 a) (Static)","0":"Creates a Vector2 entity with x and y elements, and then returns it."}]

# Operators

[{"index":"Vector3 Addition(Vector3 left, Vector3 right) (Static)","0":"Returns a new Vector3 value obtained by adding two vectors."}]

[{"index":"Vector3 Division(Vector3 left, float right) (Static)","0":"Returns a new Vector3 value by dividing the vector by a real number."}]

[{"index":"boolean Equality(Vector3 left, Vector3 right) (Static)","0":"Checks if the two vectors are the same. Returns true if they're the same. Otherwise, returns false."}]

[{"index":"boolean Inequality(Vector3 left, Vector3 right) (Static)","0":"Checks if the two vectors are different. Returns false if they're the same. Otherwise, returns true."}]

[{"index":"Vector3 Multiply(float left, Vector3 right) (Static)","0":"Returns a new Vector3 entity by multiplying the vector by a real number."}]

[{"index":"Vector3 Multiply(Vector3 left, float right) (Static)","0":"Returns a new Vector3 entity by multiplying the vector by a real number."}]

[{"index":"Vector3 Subtraction(Vector3 left, Vector3 right) (Static)","0":"Returns a new Vector3 entity obtained by subtracting two vectors."}]

[{"index":"Vector3 UnaryNegation(Vector3 value) (Static)","0":"Returns a new Vector3 entity that represents a reversed vector."}]

# Examples

#### Vector Creation and Reference Behavior

Vectors behave similarly to reference types in general programming languages.
    
    
    @Component
    script PositionInitializer extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            local v = Vector3(1.0, 2.0, 3.0)
            
            local position = self.Entity.Transform.Position
            position.x = 10
            position = v
        end
    end
    

If the code above is put into OnBeginPlay and executed, the x-coordinate of the Entity moves to 10, but not to (1.0, 2.0, 3.0). If you want to move to (1.0, 2.0, 3.0), use one of the following three methods:
    
    
    @Component
    script InitialPositionSetter extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            -- 1.
            local v = Vector3(1.0, 2.0, 3.0)
            self.Entity.Transform.Position = v
            
            -- 2.
            self.Entity.Transform.Position = Vector3(1.0, 2.0, 3.0)
            
            -- 3.
            local position = self.Entity.Transform.Position
            position.x = 1.0
            position.y = 2.0
            position.z = 3.0
        end
    end
    

#### Constant Vector and Operations

Default unit vectors of Vector, such as Vector3.up and Vector3.forward, are supported. In the case of Vector3, there are a total of 6 types.

  * and - operations are possible between vectors, and * and / operations are possible between vectors and numbers.



Operations like += are not supported by Lua.
    
    
    @Component
    script TransformInitializer extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            self.Entity.Transform.Position = self.Entity.Transform.Position + Vector3.up * 10
            self.Entity.Transform.Scale = Vector3.one * 2
        end
    end
    

#### Vector Functions

Provides commonly used basic functions in Vector operation. These include Distance, Dot, Magnitude, Normalize, etc. Note that when creating a 2D game, positions of most functions should be converted into Vector2, unless there is a specific reason to do otherwise.
    
    
    @Component
    script TwoDDistanceLogger extends Component
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
            local myPos = self.Entity.Transform.Position
            local otherPos = _EntityService:GetEntityByPath(EntityPath)
            
            local myPosV2 = Vector2(myPos.x, myPos.y)
            local otherPosV2 = Vector2(otherPos.x, otherPos.y)
            
            local distance = Vector2.Distance(myPosV2, otherPosV2)
            log("2D distance", distance)
        end
    end
    

# SeeAlso

  * TransformComponent
  * EntityService