Class MathUtil
Provides advanced mathematical utilities for game development including coordinate conversions, physics-based smoothing, geometry calculations, and camera frustum operations.
Inherited Members
Namespace: Scylla.Core.Util.Math
Assembly: ScyllaCore.dll
Syntax
public static class MathUtil
Remarks
MathUtil focuses on higher-level spatial mathematics that are frequently
needed in game logic, camera systems, and physics integrations:
- Exponential/power functions for non-integer exponents.
- Normalization and denormalization between arbitrary ranges and [0, 1].
- Angle clamping and wrapping to the [-PI, PI] range.
- Bidirectional conversion between Cartesian, spherical, cylindrical, and polar coordinate systems.
- Vector flattening and rotation correction helpers.
- Spring-damper smooth damping and constant-speed convergence for value animation.
- 3D geometry utilities: plane intersection, grid snapping, plane projection, and closest-point-on-segment queries.
- Legacy camera frustum corner extraction (see Scylla.Core.Util.Math.MathUtil.GetNearPlaneCorners(UnityEngine.Camera) for the deprecation note).
For basic numeric operations such as clamping, rounding, floating-point comparison, and safe division, see NumberUtil.
Methods
CartesianToCylindrical(Vector3)
Converts a 3D Cartesian position to cylindrical coordinates (r, theta, height).
Declaration
public static Vector3 CartesianToCylindrical(Vector3 cartesian)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | cartesian | The Cartesian position (x, y, z) to convert. The y component represents height and is passed through unchanged. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 encoding the cylindrical coordinates:
|
Remarks
Unity's coordinate system places the vertical axis on Y. The cylindrical
azimuthal angle theta is therefore measured in the XZ plane, not XY.
To convert back, use CylindricalToCartesian(Vector3).
See Also
CartesianToPolar(Vector2)
Converts a 2D Cartesian position to polar coordinates, with the angle measured from the positive X axis.
Declaration
public static Vector2 CartesianToPolar(Vector2 cartesian)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | cartesian | The 2D Cartesian position (x, y) to convert. The zero vector produces a radius
of |
Returns
| Type | Description |
|---|---|
| Vector2 | A UnityEngine.Vector2 encoding the polar coordinates:
|
Remarks
This overload follows the standard mathematical convention where the reference direction is the positive X axis. This differs from the 3D overload CartesianToPolar(Vector3), which measures the angle from the positive Z axis in the XZ plane.
See Also
CartesianToPolar(Vector3)
Converts a 3D Cartesian position to 2D polar coordinates by projecting onto the XZ plane and ignoring the Y component.
Declaration
public static Vector2 CartesianToPolar(Vector3 cartesian)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | cartesian | The 3D Cartesian position. The |
Returns
| Type | Description |
|---|---|
| Vector2 | A UnityEngine.Vector2 encoding the polar coordinates:
|
Remarks
This overload is useful for top-down or strategy-style calculations where vertical height is irrelevant. For a full 2D polar conversion from a UnityEngine.Vector2 measured from the positive X axis, use CartesianToPolar(Vector2).
See Also
CartesianToSpherical(Vector3)
Converts a 3D Cartesian position to spherical coordinates (rho, theta, phi).
Declaration
public static Vector3 CartesianToSpherical(Vector3 cartesian)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | cartesian | The Cartesian position (x, y, z) to convert. The origin (zero vector) returns UnityEngine.Vector3.zero to avoid a division-by-zero when computing angles. |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 encoding the spherical coordinates:
|
Remarks
The convention used here follows the physics/ISO convention where phi
is the polar (colatitude) angle from the Y axis, not the elevation angle from
the XZ plane. This is the inverse of SphericalToCartesian(Vector3).
If cartesian is the zero vector (magnitude below
float.Epsilon), the method returns UnityEngine.Vector3.zero rather
than computing undefined angular values.
See Also
ClosestPointOnLine(Vector3, Vector3, Vector3)
Finds the closest point on a line segment to a given point.
Declaration
public static Vector3 ClosestPointOnLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The world-space point for which to find the nearest position on the segment. |
| Vector3 | lineStart | The start endpoint of the line segment. |
| Vector3 | lineEnd | The end endpoint of the line segment. If |
Returns
| Type | Description |
|---|---|
| Vector3 | The point on the segment [ |
Remarks
The parameter t along the segment is computed as the dot product of the
offset vector and the segment direction, divided by the squared segment length,
then clamped to [0, 1] to restrict the result to the segment rather than the
infinite line.
ConvergeToValue(float, float, float, float)
Moves current toward target at a constant
rate of speed units per second, clamping at the target so the
value never overshoots.
Declaration
public static float ConvergeToValue(float target, float current, float deltaTime, float speed)
Parameters
| Type | Name | Description |
|---|---|---|
| float | target | The value to converge toward. May be greater than, less than, or equal to
|
| float | current | The current value before the step is applied. |
| float | deltaTime | The elapsed time since the last call, in seconds. Typically
|
| float | speed | The convergence rate in units per second. Must be non-negative; a value of
|
Returns
| Type | Description |
|---|---|
| float | The new value after advancing by |
Remarks
Unlike SmoothDamp(float, float, ref float, float, float, float), this method moves at a constant linear speed rather than applying a spring-damper model. It is useful for UI meters, health bars, or any animation that should arrive at its target in a predictable, frame-rate-independent time.
See Also
CorrectRotationUp(ref Quaternion)
Corrects a quaternion rotation so that its derived up vector aligns with the world up vector (UnityEngine.Vector3.up), while preserving the forward direction.
Declaration
public static void CorrectRotationUp(ref Quaternion rotation)
Parameters
| Type | Name | Description |
|---|---|---|
| Quaternion | rotation | The rotation to correct. Modified in place. If the forward vector of
|
Remarks
This method is particularly useful for stabilizing character or camera rotations that have accumulated roll due to quaternion interpolation or physics integration. It reconstructs the rotation matrix from scratch using the Gram-Schmidt orthogonalization approach: forward is kept, right is computed as the cross product of world up and forward, and the corrected up is computed as the cross product of forward and right.
The edge case - when forward is nearly collinear with world up, such as when a character looks straight up or down - is handled by substituting a safe fallback forward so that the resulting right vector remains valid.
CylindricalToCartesian(Vector3)
Converts cylindrical coordinates (r, theta, height) to a 3D Cartesian position.
Declaration
public static Vector3 CylindricalToCartesian(Vector3 cylindrical)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | cylindrical | The cylindrical coordinates encoded as a UnityEngine.Vector3:
|
Returns
| Type | Description |
|---|---|
| Vector3 | The Cartesian position (x, y, z) corresponding to the given cylindrical coordinates. The y component of the result is the height value from the input z component. |
See Also
Denormalize(float, float, float)
Converts a normalized value in [0, 1] back to the range
[min, max].
Declaration
public static float Denormalize(float normalized, float min, float max)
Parameters
| Type | Name | Description |
|---|---|---|
| float | normalized | The normalized value to map into the output range. Values outside [0, 1] are
clamped before the conversion, so the result is always within
[ |
| float | min | The lower bound of the output range. A |
| float | max | The upper bound of the output range. A |
Returns
| Type | Description |
|---|---|
| float | A value in [ |
See Also
ExpDecay(float, float, float, float)
Moves current toward target using
framerate-independent exponential decay. Unlike a plain lerp with a constant
factor, the result is identical whether the same wall-clock time is simulated
in one large step or many small ones.
Declaration
public static float ExpDecay(float current, float target, float lambda, float deltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| float | current | The current value. |
| float | target | The value to approach. |
| float | lambda | The decay rate per second. Higher values converge faster. The remaining
distance halves every |
| float | deltaTime | The elapsed time since the previous call, in seconds. |
Returns
| Type | Description |
|---|---|
| float | The decayed value: |
Remarks
Prefer this over a constant-factor lerp in per-frame smoothing code: the constant-factor lerp converges at different speeds depending on the framerate, while exponential decay does not. For spring-like motion with velocity continuity, use SmoothDamp(float, float, ref float, float, float, float) instead.
See Also
ExpDecay(Quaternion, Quaternion, float, float)
Rotates current toward target using
framerate-independent exponential decay. Quaternion counterpart of
ExpDecay(float, float, float, float), implemented as a spherical
interpolation with a decay-derived factor.
Declaration
public static Quaternion ExpDecay(Quaternion current, Quaternion target, float lambda, float deltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| Quaternion | current | The current rotation. |
| Quaternion | target | The rotation to approach. |
| float | lambda | The decay rate per second. Higher values converge faster. The remaining
angular distance halves every |
| float | deltaTime | The elapsed time since the previous call, in seconds. |
Returns
| Type | Description |
|---|---|
| Quaternion | The rotation advanced toward the target by the factor
|
See Also
ExpDecay(Vector3, Vector3, float, float)
Moves current toward target using
framerate-independent exponential decay. Vector counterpart of
ExpDecay(float, float, float, float); all three components decay
with the same factor, so the motion follows a straight line toward the target.
Declaration
public static Vector3 ExpDecay(Vector3 current, Vector3 target, float lambda, float deltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | current | The current value. |
| Vector3 | target | The value to approach. |
| float | lambda | The decay rate per second. Higher values converge faster. The remaining
distance halves every |
| float | deltaTime | The elapsed time since the previous call, in seconds. |
Returns
| Type | Description |
|---|---|
| Vector3 | The decayed value: |
See Also
ExpN(float, float)
Calculates x raised to the power of n using
exponential and natural-logarithm functions, supporting non-integer exponents.
Declaration
public static float ExpN(float x, float n)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | The base value. Must be strictly positive ( |
| float | n | The exponent. May be any real number including fractional and negative values. |
Returns
| Type | Description |
|---|---|
| float | The result of x^n computed as |
Remarks
This method is equivalent to Mathf.Pow(x, n) for positive bases but is
provided for cases where the caller explicitly works in the exp/log domain (for
example, chaining multiple power operations without intermediate clamping).
For integer exponents over a positive base, Mathf.Pow is equivalent and
may be marginally faster.
FlattenXZ(Vector3)
Projects v onto the XZ plane (sets Y to zero), then returns
the normalized result.
Declaration
public static Vector3 FlattenXZ(Vector3 v)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | v | The input vector. The Y component is discarded before normalization. If the
resulting XZ vector has a squared magnitude below |
Returns
| Type | Description |
|---|---|
| Vector3 | A unit vector in the XZ plane pointing in the same horizontal direction as
|
Remarks
Commonly used in character controller and camera code to extract a horizontal movement direction from a 3D velocity without the vertical component influencing the direction or causing normalization artifacts. Use FlattenXZRaw(Vector3) if the raw un-normalized XZ vector is needed.
See Also
FlattenXZRaw(Vector3)
Projects v onto the XZ plane by setting Y to zero, without
normalizing the result.
Declaration
public static Vector3 FlattenXZRaw(Vector3 v)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | v | The input vector. The X and Z components are preserved unchanged; only Y is zeroed. |
Returns
| Type | Description |
|---|---|
| Vector3 | A copy of |
Remarks
Use this overload when the original XZ magnitude must be retained (for example, when extracting horizontal velocity from a physics body). For a normalized horizontal direction, use FlattenXZ(Vector3) instead.
See Also
Intersection3Planes(Plane, Plane, Plane)
Calculates the unique intersection point of three planes.
Declaration
public static Vector3? Intersection3Planes(Plane p0, Plane p1, Plane p2)
Parameters
| Type | Name | Description |
|---|---|---|
| Plane | p0 | The first plane. |
| Plane | p1 | The second plane. |
| Plane | p2 | The third plane. |
Returns
| Type | Description |
|---|---|
| Vector3? | The world-space point where all three planes meet, or |
Remarks
The intersection is computed using Cramer's rule applied to the linear system formed by the three plane equations:
P = (-d0 * (n1 × n2) - d1 * (n2 × n0) - d2 * (n0 × n1)) / (n0 · (n1 × n2))
where d is the signed distance from the plane to the origin as stored in
Plane.distance, and n is the plane normal.
See Also
Logistic(float, float, float)
Evaluates the standard logistic function, a smooth S-shaped curve that maps any real input into the open interval (0, 1).
Declaration
public static float Logistic(float x, float steepness = 1, float midpoint = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | The input value. |
| float | steepness | Controls how sharply the curve transitions around |
| float | midpoint | The input value at which the output is exactly |
Returns
| Type | Description |
|---|---|
| float |
|
Remarks
The logistic curve is exponential and approaches its bounds asymptotically, so it does
not pass through (0, 0) or (1, 1) exactly. Callers needing a curve pinned
to those endpoints (for example a response curve over a normalized [0, 1] input) should
renormalize the output against Logistic(0, ...) and Logistic(1, ...).
See Also
Normalize(float, float, float)
Normalizes value from the range
[min, max] to [0, 1].
Declaration
public static float Normalize(float value, float min, float max)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | The value to normalize. Values outside [ |
| float | min | The minimum bound of the input range, mapping to |
| float | max | The maximum bound of the input range, mapping to |
Returns
| Type | Description |
|---|---|
| float | A value in [0, 1] representing the normalized position of
|
See Also
PolarToCartesian(Vector2)
Converts 2D polar coordinates to a 2D Cartesian position, with the angle measured from the positive X axis.
Declaration
public static Vector2 PolarToCartesian(Vector2 polar)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | polar | The polar coordinates encoded as a UnityEngine.Vector2:
|
Returns
| Type | Description |
|---|---|
| Vector2 | The 2D Cartesian position (x, y) corresponding to the given polar coordinates. |
Remarks
This is the inverse of CartesianToPolar(Vector2). The angle convention (measured from positive X) matches the 2D overload of CartesianToPolar(Vector2), not the 3D one.
See Also
PolarToCartesian3D(Vector2)
Converts 2D polar coordinates to a 3D Cartesian position in the XZ plane, with the Y component set to zero.
Declaration
public static Vector3 PolarToCartesian3D(Vector2 polar)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | polar | The polar coordinates encoded as a UnityEngine.Vector2:
|
Returns
| Type | Description |
|---|---|
| Vector3 | A 3D Cartesian position (x, 0, z) in the XZ plane. The Y component is always
|
Remarks
This is the inverse of CartesianToPolar(Vector3). The angle convention (measured from positive Z) matches the 3D overload, not the 2D one.
See Also
ProjectOnPlane(Vector3, Plane)
Projects a point in 3D space onto a plane, returning the nearest point on the plane's surface.
Declaration
public static Vector3 ProjectOnPlane(Vector3 point, Plane plane)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | The world-space point to project. Any point is valid, including points already on the plane (which are returned unchanged within floating-point precision). |
| Plane | plane | The plane to project onto. The plane's normal must be normalized; |
Returns
| Type | Description |
|---|---|
| Vector3 | The point on |
Remarks
This is equivalent to Vector3.ProjectOnPlane(point - origin, plane.normal)
- origin but uses the
Planestruct's built-in distance API for clarity.
Sigmoid(float, float, float)
Evaluates a bounded algebraic sigmoid, a smooth S-shaped curve that maps any real input into [0, 1] without using an exponential.
Declaration
public static float Sigmoid(float x, float steepness = 1, float midpoint = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | The input value. |
| float | steepness | Controls how sharply the curve transitions around |
| float | midpoint | The input value at which the output is exactly |
Returns
| Type | Description |
|---|---|
| float |
|
Remarks
This algebraic form avoids Mathf.Exp and is cheaper than Logistic(float, float, float)
while producing a similar shape. Like the logistic curve it is asymptotic, so it does
not reach 0 or 1 for finite input.
See Also
SmoothDamp(float, float, ref float, float, float, float)
Smoothly damps current toward target using
a critically damped spring model, equivalent to Unity's
Mathf.SmoothDamp.
Declaration
public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| float | current | The current value at the start of this step. |
| float | target | The value to approach. The function clamps how far the value can move per step
based on |
| float | currentVelocity | The current velocity of the smoothed value, maintained across calls to produce
continuous motion. Initialize to |
| float | smoothTime | The approximate time (in seconds) for the value to reach the target under
ideal conditions. Smaller values produce a faster, snappier response. Must be
positive; values below |
| float | maxSpeed | The maximum rate of change per second. Use |
| float | deltaTime | The elapsed time since the previous call, in seconds. Typically
|
Returns
| Type | Description |
|---|---|
| float | The smoothed value after advancing one step toward the target. The value will not overshoot the target; if the computed result would pass the target, it is clamped to the target and the velocity is reset accordingly. |
Remarks
This is a custom implementation of the same critically damped spring
algorithm used by Unity's Mathf.SmoothDamp. It is provided here so
that game systems that do not directly reference Mathf can use the
same algorithm through the Scylla utility layer.
The polynomial approximation for the exponential term (exp ≈ 1 / (1 + x
+ 0.48x² + 0.235x³)) is the same approximation used by Unity and
produces results accurate enough for all game use cases while avoiding a
call to Mathf.Exp.
See Also
SmoothDamp(Vector3, Vector3, ref Vector3, float, float, float)
Smoothly damps current toward target using
a critically damped spring model, equivalent to Unity's
Vector3.SmoothDamp. This is the vector counterpart of the scalar
SmoothDamp(float, float, ref float, float, float, float) overload.
Declaration
public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | current | The current position or value at the start of this step. |
| Vector3 | target | The value to approach. The per-step change is magnitude-clamped based on
|
| Vector3 | currentVelocity | The current velocity of the smoothed value, maintained across calls to produce
continuous motion. Initialize to |
| float | smoothTime | The approximate time (in seconds) for the value to reach the target under ideal
conditions. Smaller values produce a faster, snappier response. Must be positive;
values below |
| float | maxSpeed | The maximum rate of change per second. Use |
| float | deltaTime | The elapsed time since the previous call, in seconds. Typically
|
Returns
| Type | Description |
|---|---|
| Vector3 | The smoothed value after advancing one step toward the target. The value does not overshoot the target; if the computed step would pass it, the result is clamped to the target and the velocity is zeroed accordingly. |
Remarks
Uses the same polynomial approximation for the exponential term
(exp ≈ 1 / (1 + x + 0.48x² + 0.235x³)) as the scalar overload and as
Unity's own implementation, applied componentwise with a vector magnitude
clamp on the per-step change.
See Also
SmoothDampAngle(float, float, ref float, float, float, float)
Smoothly damps an angle in degrees toward a target angle using the critically damped spring model of SmoothDamp(float, float, ref float, float, float, float), taking the shortest path around the circle (correctly crossing the 0/360 boundary).
Declaration
public static float SmoothDampAngle(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
Parameters
| Type | Name | Description |
|---|---|---|
| float | current | The current angle in degrees. |
| float | target | The target angle in degrees. |
| float | currentVelocity | The current angular velocity in degrees per second, maintained across calls.
Initialize to |
| float | smoothTime | The approximate time (in seconds) for the angle to reach the target. Must be
positive; values below |
| float | maxSpeed | The maximum angular rate of change in degrees per second. Use
|
| float | deltaTime | The elapsed time since the previous call, in seconds. |
Returns
| Type | Description |
|---|---|
| float | The smoothed angle in degrees after advancing one step toward the target. The
returned angle is continuous with |
See Also
SnapToGrid(Vector2, float)
Snaps a 2D position to the nearest point on a uniform 2D grid.
Declaration
public static Vector2 SnapToGrid(Vector2 position, float gridSize)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | position | The position to snap. Each component is independently rounded to the nearest
multiple of |
| float | gridSize | The cell size of the grid. Must be greater than zero; a value of zero or less
returns |
Returns
| Type | Description |
|---|---|
| Vector2 | A UnityEngine.Vector2 with each component rounded to the nearest multiple of
|
See Also
SnapToGrid(Vector3, float)
Snaps a 3D position to the nearest point on a uniform 3D grid.
Declaration
public static Vector3 SnapToGrid(Vector3 position, float gridSize)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | position | The world-space position to snap. Each component is independently rounded to
the nearest multiple of |
| float | gridSize | The cell size of the grid. Must be greater than zero; a value of zero or less
returns |
Returns
| Type | Description |
|---|---|
| Vector3 | A UnityEngine.Vector3 with each component rounded to the nearest multiple of
|
See Also
SphericalToCartesian(Vector3)
Converts spherical coordinates (rho, theta, phi) to a 3D Cartesian position.
Declaration
public static Vector3 SphericalToCartesian(Vector3 spherical)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | spherical | The spherical coordinates encoded as a UnityEngine.Vector3:
|
Returns
| Type | Description |
|---|---|
| Vector3 | The Cartesian position (x, y, z) corresponding to the given spherical coordinates. |
See Also
Swap<T>(ref T, ref T)
Swaps the values of two variables in place.
Declaration
public static void Swap<T>(ref T a, ref T b)
Parameters
| Type | Name | Description |
|---|---|---|
| T | a | The first variable. Receives the original value of |
| T | b | The second variable. Receives the original value of |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the values to swap. Any type is accepted; value types are swapped by copy and reference types have their references exchanged. |
ToCartesian(float, float)
Converts spherical yaw and pitch angles to a unit Cartesian direction vector and returns the result.
Declaration
public static Vector3 ToCartesian(float yaw, float pitch)
Parameters
| Type | Name | Description |
|---|---|---|
| float | yaw | The horizontal rotation angle (azimuth) around the Y axis, in radians. A value
of |
| float | pitch | The vertical elevation angle, in radians. A value of |
Returns
| Type | Description |
|---|---|
| Vector3 | The unit direction vector in Cartesian (x, y, z) coordinates corresponding to the given yaw and pitch. |
Remarks
This overload is a convenience wrapper that delegates to
ToCartesian(float, float, out Vector3). Use the out overload
when writing into an existing variable to avoid an extra copy.
See Also
ToCartesian(float, float, out Vector3)
Converts spherical yaw and pitch angles to a unit Cartesian direction vector,
writing the result to the direction out parameter.
Declaration
public static void ToCartesian(float yaw, float pitch, out Vector3 direction)
Parameters
| Type | Name | Description |
|---|---|---|
| float | yaw | The horizontal rotation angle (azimuth) around the Y axis, in radians. A value
of |
| float | pitch | The vertical elevation angle, in radians. A value of |
| Vector3 | direction | Receives the resulting unit direction vector in Cartesian (x, y, z) coordinates. |
See Also
ToPIRange(float)
Wraps an angle in radians to the canonical range [-PI, PI] and returns the result.
Declaration
public static float ToPIRange(float angle)
Parameters
| Type | Name | Description |
|---|---|---|
| float | angle | The angle in radians to wrap. May be any finite value including large
multi-revolution angles such as |
Returns
| Type | Description |
|---|---|
| float | The equivalent angle expressed in the range [-PI, PI]. |
Remarks
This overload is a convenience wrapper that delegates to
ToPIRange(ref float). Use the ref overload when modifying an
existing variable to avoid a temporary copy.
See Also
ToPIRange(ref float)
Wraps an angle in radians to the canonical range [-PI, PI] in place.
Declaration
public static void ToPIRange(ref float angle)
Parameters
| Type | Name | Description |
|---|---|---|
| float | angle | The angle in radians to wrap. Receives the wrapped result directly; values already within [-PI, PI] are still processed by the modulo but are unaffected in practice. |
Remarks
The method first applies a modulo by 2PI to collapse large multi-revolution values, then adjusts values outside (-PI, PI] by one full revolution. This is useful for maintaining angles in camera and character rotation systems where crossing the ±PI boundary would otherwise cause discontinuities.
See Also
ToSpherical(Vector3, out float, out float)
Converts a 3D Cartesian direction vector to spherical yaw and pitch angles.
Declaration
public static void ToSpherical(Vector3 direction, out float yaw, out float pitch)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | direction | The Cartesian direction vector to convert. Does not need to be normalized; only
the direction (ratio of components) is used. A zero vector produces yaw and pitch
values of |
| float | yaw | Receives the horizontal rotation angle (azimuth) around the Y axis, in radians. Measured from the positive Z axis toward the positive X axis, in the range (-PI, PI]. |
| float | pitch | Receives the vertical elevation angle, in radians. Positive values tilt upward (toward positive Y), in the range [-PI/2, PI/2]. |
Remarks
This overload produces the yaw and pitch angles used by typical first-person and orbit camera systems. To reconstruct the direction vector from yaw and pitch, call ToCartesian(float, float, out Vector3).