Skip to content

Vector2 Extension

Andrzej Kebab edited this page Feb 2, 2024 · 1 revision

UtilityLibrary.Unity.Runtime

Deconstruct

Deconstruct

Deconstructs a Vector2 instance into its individual components.

Vector2 vector = // initialize your Vector2
float x, y;
vector.Deconstruct(out x, out y);

Parameters:

  • vector: The Vector2 instance.

Outputs:

  • x: The x component of the Vector2.
  • y: The y component of the Vector2.

With

With

Creates a new Vector2 instance with the specified components, or the original components if not specified.

Vector2 originalVector = // initialize your Vector2
float? x = // optional
float? y = // optional
Vector2 newVector = originalVector.With(x, y);

Parameters:

  • originalVector: The original Vector2 instance.
  • x: The x component of the new Vector2. If null, the x component of the original Vector2 is used.
  • y: The y component of the new Vector2. If null, the y component of the original Vector2 is used.

Returns:

  • newVector: A new Vector2 instance with the specified components.

Distance2Line

Distance2Line

Calculates the shortest distance from a Vector2 to a line segment.

Vector2 vector = // initialize your Vector2
Vector2 linePoint1 = // initialize your first line point
Vector2 linePoint2 = // initialize your second line point
float distance = vector.Distance2Line(linePoint1, linePoint2);

Parameters:

  • vector: The Vector2 instance.
  • linePoint1: The first point of the line segment.
  • linePoint2: The second point of the line segment.

Returns:

  • distance: The shortest distance from the Vector2 to the line segment.

Rotate

Rotate

Rotates a point around an origin point by a specified angle along a specified axis.

Vector2 origin = // initialize your origin point
Vector2 point = // initialize the point to rotate
Vector2 axis = // initialize the rotation axis
float angle = // initialize the rotation angle
Vector2 rotatedPoint = origin.Rotate(point, axis, angle);

Parameters:

  • origin: The origin point for the rotation.
  • point: The point to rotate.
  • axis: The axis to rotate the point along.
  • angle: The angle to rotate the point by.

Returns:

  • rotatedPoint: The rotated point.

Flip

Flip

Flips a Vector2 in both the x and y directions.

Vector2 vector = // initialize your Vector2
Vector2 flippedVector = vector.Flip();

Parameters:

  • vector: The Vector2 instance.

Returns:

  • flippedVector: The flipped Vector2.

FlipX

Flips a Vector2 in the x direction.

Vector2 vector = // initialize your Vector2
Vector2 flippedVector = vector.FlipX();

Parameters:

  • vector: The Vector2 instance.

Returns:

  • flippedVector: The flipped Vector2.

FlipY

Flips a Vector2 in the y direction.

Vector2 vector = // initialize your Vector2
Vector2 flippedVector = vector.FlipY();

Parameters:

  • vector: The Vector2 instance.

Returns:

  • flippedVector: The flipped Vector2.

Clamp01

Clamps the components of a Vector2 between 0 and 1.

Vector2 vector = // initialize your Vector2
Vector2 clampedVector = vector.Clamp01();

Parameters:

  • vector: The Vector2 instance.

Returns:

  • clampedVector: The clamped Vector2.

Clamp

Clamp

Clamps the components of a Vector2 between specified minimum and maximum values.

Vector2 vector = // initialize your Vector2
float min = // initialize the minimum value
float max = // initialize the maximum value
Vector2 clampedVector = vector.Clamp(min, max);

Parameters:

  • vector: The Vector2 instance.
  • min: The minimum value to clamp the components to.
  • max: The maximum value to clamp the components to.

Returns:

  • clampedVector: The clamped Vector2.

Clamp

Clamps the components of a Vector2 between specified minimum and maximum vectors.

Vector2 vector = // initialize your Vector2
Vector2 min = // initialize the minimum vector
Vector2 max = // initialize the maximum vector
Vector2 clampedVector = vector.Clamp(min, max);

Parameters:

  • vector: The Vector2 instance.
  • min: The minimum vector to clamp the components to.
  • max: The maximum vector to clamp the components to.

Returns:

  • clampedVector: The clamped Vector2.

IsNaN

Checks if a Vector2 contains NaN values.

Vector2 vector = // initialize your Vector2
bool isNaN = vector.IsNaN();

Parameters:

  • vector: The Vector2 instance.

Returns:

  • isNaN: True if the Vector2 contains NaN values, false otherwise.

Clone this wiki locally