docs.unity3d.com
    Show / Hide Table of Contents

    Class MathUtilities

    Contains various math utility functions

    Inheritance
    Object
    MathUtilities
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Unity.CharacterController
    Syntax
    public static class MathUtilities

    Methods

    AngleRadians(float3, float3)

    Calculates angles in radians between two normalized direction vectors

    Declaration
    public static float AngleRadians(float3 from, float3 to)
    Parameters
    Type Name Description
    float3 from

    The source direction

    float3 to

    The destination direction

    Returns
    Type Description
    Single

    Angle in radians

    AngleRadiansToDotRatio(Single)

    Calculates the dot product between two normalized direction vectors that are at a specified angle (in radians) from each other

    Declaration
    public static float AngleRadiansToDotRatio(float angleRadians)
    Parameters
    Type Name Description
    Single angleRadians

    The angle in radians separating the two fictional direction vectors

    Returns
    Type Description
    Single

    The dot product result

    CalculatePointDisplacement(float3, RigidTransform, RigidTransform)

    Calculates the displacement vector of a worldspace point from one transform pose to the next

    Declaration
    public static float3 CalculatePointDisplacement(float3 pointWorldSpace, RigidTransform fromTransform, RigidTransform toTransform)
    Parameters
    Type Name Description
    float3 pointWorldSpace

    The worldspace point that gets moved (before movement)

    RigidTransform fromTransform

    The original transform pose

    RigidTransform toTransform

    The destination transform pose

    Returns
    Type Description
    float3

    CalculatePointDisplacementFromVelocity(Single, RigidTransform, float3, float3, float3)

    Calculates the displacement vector of a worldspace point relatively to a transform that moves with linear/angular velocity

    Declaration
    public static float3 CalculatePointDisplacementFromVelocity(float deltaTime, RigidTransform bodyRigidTransform, float3 linearVelocity, float3 angularVelocity, float3 pointWorldSpace)
    Parameters
    Type Name Description
    Single deltaTime

    The time delta of the movement

    RigidTransform bodyRigidTransform

    The transform pose of the moving body

    float3 linearVelocity

    The linear velocity of the moving body

    float3 angularVelocity

    The angular velocity of the moving body

    float3 pointWorldSpace

    The worldspace point to move

    Returns
    Type Description
    float3

    ClampToMaxLength(float3, Single)

    Clamps a vector to a maximum length

    Declaration
    public static float3 ClampToMaxLength(float3 vector, float maxLength)
    Parameters
    Type Name Description
    float3 vector

    The vector to clamp

    Single maxLength

    The max length to clamp to

    Returns
    Type Description
    float3

    The clamped vector

    CreateRotationWithUpPriority(float3, float3)

    Builds a rotation that prioritizes having its up direction aligned with the designated up direction. Then, it orients its forwards towards the designated forward direction as much as is can without breaking the primary up direction constraint

    Declaration
    public static quaternion CreateRotationWithUpPriority(float3 up, float3 forward)
    Parameters
    Type Name Description
    float3 up

    The target up direction

    float3 forward

    The target forward direction

    Returns
    Type Description
    quaternion

    The resulting rotation

    DotRatioToAngleRadians(Single)

    Calculates the angles in radians that represent the angle difference between two normalized direction vectors that would have a specified dot product result

    Declaration
    public static float DotRatioToAngleRadians(float dotRatio)
    Parameters
    Type Name Description
    Single dotRatio

    The dot product result between the two fictional normalized direction vectors

    Returns
    Type Description
    Single

    The angle in radians

    FromToRotation(quaternion, quaternion)

    Calculates a rotation delta from a certain rotation to another

    Declaration
    public static quaternion FromToRotation(quaternion from, quaternion to)
    Parameters
    Type Name Description
    quaternion from

    The source rotation

    quaternion to

    The destination rotation

    Returns
    Type Description
    quaternion

    The rotation delta

    GetAxisSystemFromForward(float3, out float3, out float3)

    Creates perpendicular right and up directions based on a given forward direction

    Declaration
    public static void GetAxisSystemFromForward(float3 fwd, out float3 right, out float3 up)
    Parameters
    Type Name Description
    float3 fwd

    The designated forward direction

    float3 right

    Outputted right direction

    float3 up

    Outputted up direction

    GetForwardFromRotation(quaternion)

    Gets the forward direction of a given quaternion

    Declaration
    public static float3 GetForwardFromRotation(quaternion rot)
    Parameters
    Type Name Description
    quaternion rot

    The rotation in quaternion

    Returns
    Type Description
    float3

    The forward direction

    GetRightFromRotation(quaternion)

    Gets the right direction of a given quaternion

    Declaration
    public static float3 GetRightFromRotation(quaternion rot)
    Parameters
    Type Name Description
    quaternion rot

    The rotation in quaternion

    Returns
    Type Description
    float3

    The right direction

    GetSharpnessInterpolant(Single, Single)

    Returns an interpolant parameter that represents interpolating with a given sharpness

    Declaration
    public static float GetSharpnessInterpolant(float sharpness, float dt)
    Parameters
    Type Name Description
    Single sharpness

    The desired interpolation sharpness

    Single dt

    The interpolation time delta

    Returns
    Type Description
    Single

    The resulting interpolant

    GetUpFromRotation(quaternion)

    Gets the up direction of a given quaternion

    Declaration
    public static float3 GetUpFromRotation(quaternion rot)
    Parameters
    Type Name Description
    quaternion rot

    The rotation in quaternion

    Returns
    Type Description
    float3

    The up direction

    ProjectOnPlane(float3, float3)

    Projects a vector on a plane

    Declaration
    public static float3 ProjectOnPlane(float3 vector, float3 onPlaneNormal)
    Parameters
    Type Name Description
    float3 vector

    The vector to project

    float3 onPlaneNormal

    The plane normal to project on

    Returns
    Type Description
    float3

    The projected vector

    ReorientVectorOnPlaneAlongDirection(float3, float3, float3)

    Reorients a vector on a plane while constraining its direction so that the resulting vector is between the original vector and the specified direction

    Declaration
    public static float3 ReorientVectorOnPlaneAlongDirection(float3 vector, float3 onPlaneNormal, float3 alongDirection)
    Parameters
    Type Name Description
    float3 vector

    The original vector to reorient

    float3 onPlaneNormal

    The plane on which the vector should be reoriented

    float3 alongDirection

    The target direction along which the vector should be reoriented

    Returns
    Type Description
    float3

    The reoriented vector

    ReverseProjectOnVector(float3, float3, Single)

    Calculates a vectorA in the direction of "onNormalizedVector", such that if vectorA was projected on the "projectedVector"'s normalized direction, it would result in "projectedVector"

    Declaration
    public static float3 ReverseProjectOnVector(float3 projectedVector, float3 onNormalizedVector, float maxLength)
    Parameters
    Type Name Description
    float3 projectedVector

    The projected vector that we want to de-project

    float3 onNormalizedVector

    The desired normalized direction of the de-projected vector

    Single maxLength

    The maximum length of the de-projected vector (de-projection can lead to very large or infinite values for near-perpendicular directions)

    Returns
    Type Description
    float3

    The resulting de-projected vector

    RotateAroundPoint(ref quaternion, ref float3, float3, quaternion)

    Applies a rotation delta to a transform's rotation, but also modifies its position so that the end result is as if the transform has rotated around a given point in order to reach the specified rotation

    Declaration
    public static void RotateAroundPoint(ref quaternion rotation, ref float3 position, float3 aroundPoint, quaternion addedRotation)
    Parameters
    Type Name Description
    quaternion rotation

    The modified rotation

    float3 position

    The modified position

    float3 aroundPoint

    The point to rotate around

    quaternion addedRotation

    The desired rotation delta

    SetRotationAroundPoint(ref quaternion, ref float3, float3, quaternion)

    Sets a transform's rotation to a target rotation, but also modifies its position so that the end result is as if the transform has rotated around a given point in order to reach the specified rotation

    Declaration
    public static void SetRotationAroundPoint(ref quaternion rotation, ref float3 position, float3 aroundPoint, quaternion targetRotation)
    Parameters
    Type Name Description
    quaternion rotation

    The modified rotation

    float3 position

    The modified position

    float3 aroundPoint

    The point to rotate around

    quaternion targetRotation

    The desired rotation

    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023