Class
- Namespace
- CoreFramework
Provides extension methods for Unity's Vector3 structure to enable additional functionality for vector operations such as modifying components, adding offsets, range checks, and conversions.
public static class Vector3Extensions
- Inheritance
-
object
Methods
Add(Vector3, float, float, float)
Adds to any x y z values of a Vector3
public static Vector3 Add(this Vector3 vector, float x = 0, float y = 0, float z = 0)
Parameters
vector
Vector3x
floaty
floatz
float
Returns
ComponentDivide(Vector3, Vector3)
Divides two Vector3 objects component-wise.
public static Vector3 ComponentDivide(this Vector3 v0, Vector3 v1)
Parameters
v0
Vector3The Vector3 object that this method extends.
v1
Vector3The Vector3 object by which v0 is divided.
Returns
- Vector3
A new Vector3 object resulting from the component-wise division.
Examples
Use ‘ComponentDivide’ to scale a game object proportionally:
myObject.transform.localScale = originalScale.ComponentDivide(targetDimensions);
This scales the object size to fit within the target dimensions while maintaining its original proportions.
Remarks
For each component in v0 (x, y, z), it is divided by the corresponding component in v1 if the component in v1 is not zero. Otherwise, the component in v0 remains unchanged.
InRangeOf(Vector3, Vector3, float)
Returns a Boolean indicating whether the current Vector3 is in a given range from another Vector3
public static bool InRangeOf(this Vector3 current, Vector3 target, float range)
Parameters
current
Vector3The current Vector3 position
target
Vector3The Vector3 position to compare against
range
floatThe range value to compare against
Returns
- bool
True if the current Vector3 is in the given range from the target Vector3, false otherwise
RandomOffset(Vector3, float)
Adds a random offset to the components of a Vector3 within the specified range.
public static Vector3 RandomOffset(this Vector3 vector, float range)
Parameters
vector
Vector3The original vector to which the random offset will be applied.
range
floatThe maximum absolute value of random offsets that can be added or subtracted to/from each component of the vector.
Returns
- Vector3
A new Vector3 with random offsets applied to its X, Y, and Z components. Each offset is in the range [-
range
,range
].
RandomPointInAnnulus(Vector3, float, float)
Computes a random point in an annulus (a ring-shaped area) based on minimum and maximum radius values around a central Vector3 point (origin).
public static Vector3 RandomPointInAnnulus(this Vector3 origin, float minRadius, float maxRadius)
Parameters
origin
Vector3The center Vector3 point of the annulus.
minRadius
floatMinimum radius of the annulus.
maxRadius
floatMaximum radius of the annulus.
Returns
- Vector3
A random Vector3 point within the specified annulus.
ToVector3(Vector2)
Converts a Vector2 to a Vector3 with a y value of 0.
public static Vector3 ToVector3(this Vector2 v2)
Parameters
v2
Vector2The Vector2 to convert.
Returns
- Vector3
A Vector3 with the x and z values of the Vector2 and a y value of 0.
With(Vector3, float?, float?, float?)
Sets any x y z values of a Vector3
public static Vector3 With(this Vector3 vector, float? x = null, float? y = null, float? z = null)
Parameters
vector
Vector3x
float?y
float?z
float?