Skip to content

Instantly share code, notes, and snippets.

@CRodriguez25
Created April 28, 2020 17:54
Show Gist options
  • Save CRodriguez25/ff5a871ba8cdb2bb45c8d84a2d5d8a84 to your computer and use it in GitHub Desktop.
Save CRodriguez25/ff5a871ba8cdb2bb45c8d84a2d5d8a84 to your computer and use it in GitHub Desktop.
A couple of useful Vector extension methods for creating Vectors based on other vectors
public static class ExtensionMethods
{
public static Vector2 CopyWith(this Vector2 vector, float? x = null, float? y = null)
{
return new Vector2(x ?? vector.x, y ?? vector.y);
}
public static Vector3 CopyWith(this Vector3 vector, float? x = null, float? y = null, float? z = null)
{
return new Vector3(x ?? vector.x, y ?? vector.y, z ?? vector.z);
}
}
// Example Usage: transform.localPosition = targetPosition.CopyWith(x: targetPosition.x + 30, y: targetPosition.y - 15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment