Advertisement
Pro_Unit

Vector3Extensions

Nov 25th, 2021
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public static class Vector3Extensions
  4. {
  5.     public static Vector3 WithX(this Vector3 value, float x)
  6.     {
  7.         value.x = x;
  8.         return value;
  9.     }
  10.  
  11.     public static Vector3 WithY(this Vector3 value, float y)
  12.     {
  13.         value.y = y;
  14.         return value;
  15.     }
  16.  
  17.     public static Vector3 WithZ(this Vector3 value, float z)
  18.     {
  19.         value.z = z;
  20.         return value;
  21.     }
  22.  
  23.     public static Vector3 AddX(this Vector3 value, float x)
  24.     {
  25.         value.x += x;
  26.         return value;
  27.     }
  28.  
  29.     public static Vector3 AddY(this Vector3 value, float y)
  30.     {
  31.         value.y += y;
  32.         return value;
  33.     }
  34.  
  35.     public static Vector3 AddZ(this Vector3 value, float z)
  36.     {
  37.         value.z += z;
  38.         return value;
  39.     }
  40.  
  41.     public static Vector2 To2(this Vector3 value) =>
  42.         value;
  43.  
  44.     public static Vector3 ToPlaneX(this Vector3 value) =>
  45.         new Vector3(0, value.y, value.z);
  46.  
  47.     public static Vector3 ToPlaneY(this Vector3 value) =>
  48.         new Vector3(value.x, 0, value.z);
  49.  
  50.     public static Vector3 ToPlaneZ(this Vector3 value) =>
  51.         new Vector3(value.x, value.y, 0);
  52.  
  53.     public static float Angle(this Vector3 from, Vector3 to) =>
  54.         Vector3.Angle(from, to);
  55.  
  56.     public static float Angle(this Vector2 from, Vector2 to) =>
  57.         Vector2.Angle(from, to);
  58.  
  59.     public static Vector3 Project(this Vector3 vector, Vector3 normal) =>
  60.         Vector3.Project(vector, normal);
  61.  
  62.     public static Vector3 ProjectOnPlane(this Vector3 vector, Vector3 normal) =>
  63.         Vector3.ProjectOnPlane(vector, normal);
  64.  
  65.     public static Vector2 Abs(this Vector2 vector) =>
  66.         new Vector2(vector.x.Abs(), vector.y.Abs());
  67.  
  68.     public static Vector3 Abs(this Vector3 vector) =>
  69.         new Vector3(vector.x.Abs(), vector.y.Abs(), vector.z.Abs());
  70.  
  71.     public static Vector4 Abs(this Vector4 vector) =>
  72.         new Vector4(vector.x.Abs(), vector.y.Abs(), vector.z.Abs(), vector.w.Abs());
  73.  
  74.     public static float GetLength(this Vector3[] path)
  75.     {
  76.         float length = 0f;
  77.         for (int i = 1; i < path.Length; i++)
  78.             length += Vector3.Distance(path[i - 1], path[i]);
  79.         return length;
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement