Skip to content

Instantly share code, notes, and snippets.

@modyari
Created September 13, 2021 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modyari/e53cefad97aebeb9a290504206a7fc61 to your computer and use it in GitHub Desktop.
Save modyari/e53cefad97aebeb9a290504206a7fc61 to your computer and use it in GitHub Desktop.
(Unity) Modifies a Vector's component and returns it
using UnityEngine;
public static class VectorExtensions
{
/// <summary>
/// Returns a modified version of the vector with one or more components modified
/// </summary>
/// <param name="x">X component to modify</param>
/// <param name="y">Y component to modify</param>
/// <returns>A modified version of the vector with the passed component configurations</returns>
public static Vector2 Modify(this Vector2 point, float? x = null, float? y = null)
{
// transform.position = transform.position.Modify(y: 0)
return new Vector2(x == null ? point.x : x.Value, y == null ? point.y : y.Value);
}
/// <summary>
/// Get a modified version of the vector with one or more components modified
/// </summary>
/// <param name="x">X component to modify</param>
/// <param name="y">Y component to modify</param>
/// <param name="z">Z component to modify</param>
/// <returns>A modified version of the vector with the passed component configurations</returns>
public static Vector3 Modify(this Vector3 point, float? x = null, float? y = null, float? z = null)
{
// transform.position = transform.position.Modify(z: 0)
return new Vector3(x == null ? point.x : x.Value, y == null ? point.y : y.Value, z == null ? point.z : z.Value);
}
/// <summary>
/// Get a modified version of the vector with one or more components modified
/// </summary>
/// <param name="x">X component to modify</param>
/// <param name="y">Y component to modify</param>
/// <param name="z">Z component to modify</param>
/// <param name="w">W component to modify</param>
/// <returns>A modified version of the vector with the passed component configurations</returns>
public static Vector4 Modify(this Vector4 point, float? x = null, float? y = null, float? z = null, float? w = null)
{
// transform.position = transform.position.Modify(w: 0)
return new Vector4(x == null ? point.x : x.Value,
y == null ? point.y : y.Value,
z == null ? point.z : z.Value,
w == null ? point.w : w.Value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment