UnityEssentials
Small but useful tools and features for Unity
VectorExtensions.cs
Go to the documentation of this file.
1 namespace UnityEngine
2 {
6  public static class VectorExtensions
7  {
12  public static Vector2 Clone(this Vector2 v)
13  {
14  return new Vector2(v.x, v.y);
15  }
16 
22  public static Vector2 WithX(this Vector2 v, float x = 0f)
23  {
24  return new Vector2(x, v.y);
25  }
26 
32  public static Vector2 WithY(this Vector2 v, float y = 0f)
33  {
34  return new Vector2(v.x, y);
35  }
36 
42  public static Vector3 ToVector3NewX(this Vector2 v, float x = 0f)
43  {
44  return new Vector3(x, v.x, v.y);
45  }
46 
52  public static Vector3 ToVector3NewY(this Vector2 v, float y = 0f)
53  {
54  return new Vector3(v.x, y, v.y);
55  }
56 
62  public static Vector3 ToVector3NewZ(this Vector2 v, float z = 0f)
63  {
64  return new Vector3(v.x, v.y, z);
65  }
66 
71  public static Vector3 Clone(this Vector3 v)
72  {
73  return new Vector3(v.x, v.y, v.z);
74  }
75 
80  public static Vector2 ToVector2WithoutX(this Vector3 v)
81  {
82  return new Vector2(v.y, v.z);
83  }
84 
89  public static Vector2 ToVector2WithoutY(this Vector3 v)
90  {
91  return new Vector2(v.x, v.z);
92  }
93 
98  public static Vector2 ToVector2WithoutZ(this Vector3 v)
99  {
100  return new Vector2(v.x, v.y);
101  }
102 
108  public static Vector3 WithX(this Vector3 v, float x = 0f)
109  {
110  return new Vector3(x, v.y, v.z);
111  }
112 
118  public static Vector3 WithY(this Vector3 v, float y = 0f)
119  {
120  return new Vector3(v.x, y, v.z);
121  }
122 
128  public static Vector3 WithZ(this Vector3 v, float z = 0f)
129  {
130  return new Vector3(v.x, v.y, z);
131  }
132 
140  public static Vector3 NearestPointOnLine(this Vector3 point, Vector3 origin, Vector3 end,
141  bool clampInsideLineLength = true)
142  {
143  //Get heading
144  Vector3 direction = (end - origin);
145  direction.Normalize();
146 
147  Vector3 lhs = point - origin;
148  float dotP = Vector3.Dot(lhs, direction);
149 
150  if (clampInsideLineLength)
151  {
152  float magnitudeMax = direction.magnitude;
153  dotP = Mathf.Clamp(dotP, 0f, magnitudeMax);
154  }
155 
156  return origin + direction * dotP;
157  }
158 
164  public static Vector3Int ToVectorInt(this Vector3 v, bool round = false)
165  {
166  if (round)
167  return new Vector3Int(Mathf.RoundToInt(v.x), Mathf.RoundToInt(v.y), Mathf.RoundToInt(v.z));
168  return new Vector3Int((int) v.x, (int) v.y, (int) v.z);
169  }
170 
176  public static Vector2Int ToVectorInt(this Vector2 v, bool round = false)
177  {
178  if (round)
179  return new Vector2Int(Mathf.RoundToInt(v.x), Mathf.RoundToInt(v.y));
180  return new Vector2Int((int) v.x, (int) v.y);
181  }
182 
183 
184  }
185 }
Extensions for Vector
static Vector2 WithX(this Vector2 v, float x=0f)
Creates a new vector with the same value for the 'y' parameter but a new one for the 'x'.
static Vector3 ToVector3NewY(this Vector2 v, float y=0f)
Creates a new Vector3 keeping the values from the 'x' and 'y' parameters of the original Vector2 in t...
static Vector3 ToVector3NewZ(this Vector2 v, float z=0f)
Creates a new Vector3 keeping the values from the 'x' and 'y' parameters of the original Vector2 in t...
static Vector2 ToVector2WithoutY(this Vector3 v)
Creates a new Vector2 keeping the values from the 'x' and 'z' parameters of the original Vector3.
static Vector3 WithZ(this Vector3 v, float z=0f)
Creates a new vector with the same value for the 'x' and 'y' parameter but a new one for the 'z'.
static Vector3 Clone(this Vector3 v)
Creates a new vector with the same values as the original.
static Vector3Int ToVectorInt(this Vector3 v, bool round=false)
Creates a new Vector3Int with the values in the original vector.
static Vector3 ToVector3NewX(this Vector2 v, float x=0f)
Creates a new Vector3 keeping the values from the 'x' and 'y' parameters of the original Vector2 in t...
static Vector3 WithY(this Vector3 v, float y=0f)
Creates a new vector with the same value for the 'x' and 'z' parameter but a new one for the 'y'.
static Vector3 WithX(this Vector3 v, float x=0f)
Creates a new vector with the same value for the 'y' and 'z' parameter but a new one for the 'x'.
static Vector3 NearestPointOnLine(this Vector3 point, Vector3 origin, Vector3 end, bool clampInsideLineLength=true)
Calculates the nearest position in a given line or segment.
static Vector2 ToVector2WithoutX(this Vector3 v)
Creates a new Vector2 keeping the values from the 'y' and 'z' parameters of the original Vector3.
static Vector2 ToVector2WithoutZ(this Vector3 v)
Creates a new Vector2 keeping the values from the 'x' and 'y' parameters of the original Vector3.
static Vector2 Clone(this Vector2 v)
Creates a new vector with the same values as the original.
static Vector2Int ToVectorInt(this Vector2 v, bool round=false)
Creates a new Vector2Int with the values in the original vector.
static Vector2 WithY(this Vector2 v, float y=0f)
Creates a new vector with the same value for the 'x' parameter but a new one for the 'y'.