Skip to content

Mathf Utilities

Description

A collection of math functions to extend the Mathf struct.

Static Methods

Method Function
RandomInsideBounds Returns a random point within the space defined by the lower and upper bounds.
Midpoint Returns the midpoint between two vectors.
ClampRotationAroundXAxis Clamps the given Quaternion's X-axis between the given minimum float and maximum float values. Returns the given value if it is within the min and max range.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// RandomInsideBounds example.
private void Foo()
{
    Vector3 botEdge = new Vector3(0, 1, 0);
    Vector3 topEdge = new Vector3(100, 1, 100);

    // Returns a random position in the world between 
    // the lower edge of the map and the upper edge.
    Vector3 spawnPos = MathfUtilities.RandomInsideBounds(botEdge, topEdge);
}

// Midpoint example.
private void Foo1()
{
    Vector3 a = new Vector3(1, 3, 1);
    Vector3 b = new Vector3(3, 1, 1);

    // (2, 2, 1)
    Vector3 mid = MathfUtilities.Midpoint(a, b); 
}

// ClampRotation example.
private void Foo2()
{
    // Prevents the X-axis value from being 
    // higher than the defined limits.
    Quaternion cameraRot = MathfUtilities.ClampRotationAroundXAxis(transform.rotation, -90, 90);
}