docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class UnityVectorExtensions

    Extensions to the Vector3 class, used by Cinemachine

    Inheritance
    object
    UnityVectorExtensions
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.Cinemachine
    Assembly: Unity.Cinemachine.dll
    Syntax
    public static class UnityVectorExtensions

    Fields

    Epsilon

    A useful Epsilon

    Declaration
    public const float Epsilon = 0.0001
    Field Value
    Type Description
    float

    Methods

    Abs(Vector2)

    Component-wise absolute value

    Declaration
    public static Vector2 Abs(this Vector2 v)
    Parameters
    Type Name Description
    Vector2 v

    Input vector

    Returns
    Type Description
    Vector2

    Component-wise absolute value of the input vector

    Abs(Vector3)

    Component-wise absolute value

    Declaration
    public static Vector3 Abs(this Vector3 v)
    Parameters
    Type Name Description
    Vector3 v

    Input vector

    Returns
    Type Description
    Vector3

    Component-wise absolute value of the input vector

    AlmostZero(Vector3)

    Is the vector within Epsilon of zero length?

    Declaration
    public static bool AlmostZero(this Vector3 v)
    Parameters
    Type Name Description
    Vector3 v
    Returns
    Type Description
    bool

    True if the square magnitude of the vector is within Epsilon of zero

    Angle(Vector3, Vector3)

    Much more stable for small angles than Unity's native implementation

    Declaration
    public static float Angle(Vector3 v1, Vector3 v2)
    Parameters
    Type Name Description
    Vector3 v1

    The first vector

    Vector3 v2

    The second vector

    Returns
    Type Description
    float

    Angle between the vectors, in degrees

    ClosestPointOnSegment(Vector2, Vector2, Vector2)

    Get the closest point on a line segment.

    Declaration
    public static float ClosestPointOnSegment(this Vector2 p, Vector2 s0, Vector2 s1)
    Parameters
    Type Name Description
    Vector2 p

    A point in space

    Vector2 s0

    Start of line segment

    Vector2 s1

    End of line segment

    Returns
    Type Description
    float

    The interpolation parameter representing the point on the segment, with 0==s0, and 1==s1

    ClosestPointOnSegment(Vector3, Vector3, Vector3)

    Get the closest point on a line segment.

    Declaration
    public static float ClosestPointOnSegment(this Vector3 p, Vector3 s0, Vector3 s1)
    Parameters
    Type Name Description
    Vector3 p

    A point in space

    Vector3 s0

    Start of line segment

    Vector3 s1

    End of line segment

    Returns
    Type Description
    float

    The interpolation parameter representing the point on the segment, with 0==s0, and 1==s1

    FindIntersection(in Vector2, in Vector2, in Vector2, in Vector2, out Vector2)

    Calculates the intersection point defined by line_1 [p1, p2], and line_2 [q1, q2].

    Declaration
    public static int FindIntersection(in Vector2 p1, in Vector2 p2, in Vector2 q1, in Vector2 q2, out Vector2 intersection)
    Parameters
    Type Name Description
    Vector2 p1

    line_1 is defined by (p1, p2)

    Vector2 p2

    line_1 is defined by (p1, p2)

    Vector2 q1

    line_2 is defined by (q1, q2)

    Vector2 q2

    line_2 is defined by (q1, q2)

    Vector2 intersection

    If lines intersect at a single point, then this will hold the intersection point. Otherwise, it will be Vector2.positiveInfinity.

    Returns
    Type Description
    int

    0 = no intersection, 1 = lines intersect, 2 = segments intersect, 3 = lines are collinear, segments do not touch, 4 = lines are collinear, segments touch (at one or at multiple points)

    IsNaN(Vector2)

    Checks if the Vector2 contains NaN for x or y.

    Declaration
    public static bool IsNaN(this Vector2 v)
    Parameters
    Type Name Description
    Vector2 v

    Vector2 to check for NaN

    Returns
    Type Description
    bool

    True, if any components of the vector are NaN

    IsNaN(Vector3)

    Checks if the Vector2 contains NaN for x or y.

    Declaration
    public static bool IsNaN(this Vector3 v)
    Parameters
    Type Name Description
    Vector3 v

    Vector2 to check for NaN

    Returns
    Type Description
    bool

    True, if any components of the vector are NaN

    IsUniform(Vector2)

    Checks whether the vector components are the same value.

    Declaration
    public static bool IsUniform(this Vector2 v)
    Parameters
    Type Name Description
    Vector2 v

    Vector to check

    Returns
    Type Description
    bool

    True, if the vector elements are the same. False, otherwise.

    IsUniform(Vector3)

    Checks whether the vector components are the same value.

    Declaration
    public static bool IsUniform(this Vector3 v)
    Parameters
    Type Name Description
    Vector3 v

    Vector to check

    Returns
    Type Description
    bool

    True, if the vector elements are the same. False, otherwise.

    NormalizeAngle(float)

    Put euler angle in the range of -180...180

    Declaration
    public static float NormalizeAngle(float angle)
    Parameters
    Type Name Description
    float angle

    The angle to normalize

    Returns
    Type Description
    float

    The angle expressed as a value -180...180

    ProjectOntoPlane(Vector3, Vector3)

    Returns a non-normalized projection of the supplied vector onto a plane as described by its normal

    Declaration
    public static Vector3 ProjectOntoPlane(this Vector3 vector, Vector3 planeNormal)
    Parameters
    Type Name Description
    Vector3 vector

    Vector to project.

    Vector3 planeNormal

    The normal that defines the plane. Must have a length of 1.

    Returns
    Type Description
    Vector3

    The component of the vector that lies in the plane

    SafeFromToRotation(Vector3, Vector3, Vector3)

    Much more stable for small angles than Unity's native implementation

    Declaration
    public static Quaternion SafeFromToRotation(Vector3 v1, Vector3 v2, Vector3 up)
    Parameters
    Type Name Description
    Vector3 v1

    The first vector

    Vector3 v2

    The second vector

    Vector3 up

    Definition of up (used to determine the sign)

    Returns
    Type Description
    Quaternion

    Rotation between the vectors

    SignedAngle(Vector3, Vector3, Vector3)

    Much more stable for small angles than Unity's native implementation

    Declaration
    public static float SignedAngle(Vector3 v1, Vector3 v2, Vector3 up)
    Parameters
    Type Name Description
    Vector3 v1

    The first vector

    Vector3 v2

    The second vector

    Vector3 up

    Definition of up (used to determine the sign)

    Returns
    Type Description
    float

    Signed angle between the vectors, in degrees

    SlerpWithReferenceUp(Vector3, Vector3, float, Vector3)

    This is a slerp that mimics a camera operator's movement in that it chooses a path that avoids the lower hemisphere, as defined by the up param

    Declaration
    public static Vector3 SlerpWithReferenceUp(Vector3 vA, Vector3 vB, float t, Vector3 up)
    Parameters
    Type Name Description
    Vector3 vA

    First direction

    Vector3 vB

    Second direction

    float t

    Interpolation amount

    Vector3 up

    Defines the up direction

    Returns
    Type Description
    Vector3

    Interpolated vector

    SquareNormalize(Vector2)

    Normalized the vector onto the unit square instead of the unit circle

    Declaration
    public static Vector2 SquareNormalize(this Vector2 v)
    Parameters
    Type Name Description
    Vector2 v

    The vector to normalize

    Returns
    Type Description
    Vector2

    The normalized vector, or the zero vector if its magnitude was too small to normalize

    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)