VectorUtil

This is a helper class for various vector operations.

REQUIREMENTS:

HOW TO USE:

  • First download the extensions library and put it inside TOZ/Plugins/Extensions folder, if you havent done already.
  • Save the below code as VectorUtil.cs and put it inside TOZ/Plugins/Utilities folder.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace TOZ {

	public static class VectorUtil {
		//returns xy components as vec3 (X)
		public static Vector3 Set3DX(Vector2 v, float a) { return new Vector3(a, v.x, v.y); }

		//returns xy components as vec3 (Y)
		public static Vector3 Set3DY(Vector2 v, float a) { return new Vector3(v.x, a, v.y); }

		//returns xy components as vec3 (Z)
		public static Vector3 Set3DZ(Vector2 v, float a) { return new Vector3(v.x, v.y, a); }

		//returns xz components as vec2
		public static Vector2 GetXZ(Vector3 v) { return new Vector2(v.x, v.z); }

		//returns xy components as vec2
		public static Vector2 GetXY(Vector3 v) { return new Vector2(v.x, v.y); }

		//returns yz components as vec2
		public static Vector2 GetYZ(Vector3 v) { return new Vector2(v.y, v.z); }

		//Reverse the direction of the 2D vector
		public static Vector2 Negate(Vector2 v) { return new Vector2(-v.x, -v.y); }

		//Reverse the direction of the 3D vector
		public static Vector3 Negate(Vector3 v) { return new Vector3(-v.x, -v.y, -v.z); }

		//Round the 3d vector elements to nearest int
		public static Vector2 Round(Vector2 v) { return new Vector2(Mathf.RoundToInt(v.x), Mathf.RoundToInt(v.y)); }

		//Round the 3d vector elements to nearest int
		public static Vector3 Round(Vector3 v) { return new Vector3(Mathf.RoundToInt(v.x), Mathf.RoundToInt(v.y), Mathf.RoundToInt(v.z)); }

		//Get point on direction at a distance
		public static Vector3 GetPoint(Vector3 origin, Vector3 direction, float distance) {
			Ray ray = new Ray(origin, direction.normalized);
			return ray.GetPoint(distance);
		}

		//Rotate Vector 2D with given angle
		public static Vector2 Rotate(Vector2 v, float angle) {
			float a = angle * Mathf.Deg2Rad;
			float sin = Mathf.Sin(a);
			float cos = Mathf.Cos(a);
			float x = (v.x * cos) - (v.y * sin);
			float y = (v.x * sin) + (v.y * cos);
			return new Vector2(x, y);
		}

		//Rotate Vector 3D on X axis (Pitch) (Gimbal Lock)
		public static Vector3 RotateX(Vector3 v, float angle) {
			float a = angle * Mathf.Deg2Rad;
			float sin = Mathf.Sin(a);
			float cos = Mathf.Cos(a);
			float x = v.x;
			float y = (v.y * cos) - (v.z * sin);
			float z = (v.z * cos) + (v.y * sin);
			return new Vector3(x, y, z);
		}

		//Rotate Vector 3D on Y axis (Yaw) (Gimbal Lock)
		public static Vector3 RotateY(Vector3 v, float angle) {
			float a = angle * Mathf.Deg2Rad;
			float sin = Mathf.Sin(a);
			float cos = Mathf.Cos(a);
			float x = (v.x * cos) + (v.z * sin);
			float y = v.y;
			float z = (v.z * cos) - (v.x * sin);
			return new Vector3(x, y, z);
		}

		//Rotate Vector 3D on Z axis (Roll) (Gimbal Lock)
		public static Vector3 RotateZ(Vector3 v, float angle) {
			float a = angle * Mathf.Deg2Rad;
			float sin = Mathf.Sin(a);
			float cos = Mathf.Cos(a);
			float x = (v.x * cos) - (v.y * sin);
			float y = (v.y * cos) + (v.x * sin);
			float z = v.z;
			return new Vector3(x, y, z);
		}

		//Rotate Vector with given angle on given axis (No Gimbal Lock)
		public static Vector3 Rotate(Vector3 v, float angle, Vector3 axis) {
			return Quaternion.AngleAxis(angle, axis) * v;
		}

		//Is the polygon not visible to line of sight?
		public static bool IsBackFace(Vector3 normal, Vector3 lookDirection) {
			return Vector3.Dot(normal, lookDirection) < 0f;
		}

		//Are the two vectors perpendicular(first vector is the normal of second)?
		public static bool IsPerpendicular(Vector3 v1, Vector3 v2) {
			return Mathf.Approximately(Vector3.Dot(v1, v2), 0f);
		}

		//returns perp product of the given 2 vec2s
		public static float Perp(Vector2 v1, Vector2 v2) {
			return (v1.x * v2.y) - (v2.x * v1.y);
		}

		//returns mixed product of the given 3 vec3s
		public static float Mixed(Vector3 v1, Vector3 v2, Vector3 v3) {
			return Vector3.Dot(Vector3.Cross(v1, v2), v3);
		}

		//returns random point around the position
		public static Vector3 RandomPos(Vector2 v, float scale) {
			return v + (RandomUtil.OnUnitCircle() * scale);
		}

		//returns random point around the position
		public static Vector3 RandomPos(Vector3 v, float scale) {
			return v + (UnityEngine.Random.onUnitSphere * scale);
		}
	}

}