The way I work with vector math is by implementing my own custom static library full of small snippets of reusable code. That lets you quickly formulate what you want to achieve, and still allows you to decouple or swap the original library later, if you want to make the code work standalone, or you want to bake-in some recurrent operations, or maybe you want to switch the process to SIMD operations in Unity.Mathematics and so on.
This is how I typically resolve component-wise division (again, this isn’t the most performant approach, but it is the most modular so I intentionally keep it transparent.)
public static class MyMath {
public static Vector3 rcp(Vector3 v) => new(1f / v.x, 1f / v.y, 1f / v.z);
public static Vector3 cmul(Vector3 a, Vector3 b) => new(a.x * b.x, a.y * b.y, a.z * b.z);
public static Vector3 cdiv(Vector3 a, Vector3 b) => cmul(a, rcp(b));
public static Vector2 rcp(Vector2 v) => new(1f / v.x, 1f / v.y);
public static Vector2 cmul(Vector2 a, Vector2 b) => new(a.x * b.x, a.y * b.y);
public static Vector2 cdiv(Vector2 a, Vector2 b) => cmul(a, rcp(b));
...
}
You then work with this like it’s Lego.
Also when converting I have general-purpose factory methods like so
public static Vector3 v3(Vector2 v, float z = 0f) => new(v.x, v.y, z);
Here’s another possibility because a static class also allows for extension methods
public static void setLocalPositionRotation(this Transform xf, Vector3 position, Quaternion rotation = Quaternion.identity) {
xf.localPosition = position;
xf.localRotation = rotation;
}
You never know when they’ll add a better, more optimized way to address this
And indeed
public static void setLocalPositionRotation(this Transform xf, Vector3 position, Quaternion rotation = Quaternion.identity)
=> xf.SetLocalPositionAndRotation(position, rotation);
Now you can compound some contextual overrides for your coding convenience in 2D
public static void setLocalPositionRotation(this Transform xf, Vector2 position, float angleInDegrees = 0f, float z = 0f) {
var rot = angleInDegrees != 0f? rotationZ(angleInDegrees) : quat_id;
xf.SetLocalPositionAndRotation(v3(position, z), rot);
}
public static Quaternion rotationZ(float angle) => Quaternion.AngleAxis(angle, v3_z);
public static readonly Vector3 v3_z = new(0f, 0f, 1f); // this must go to the top of the static class
public static readonly Quaternion quat_id = new(0f, 0f, 0f, 1f); // this as well
Which boils down to a very compact yet readable line in your production code
using UnityEngine;
using static MyMath; // now you can call all its functions without invoking MyMath all the time
public class MyClass {
void Update() { // or whatever
Vector2 my2DPosition = cdiv(vector1, vector2);
myObj.transform.setLocalPositionRotation(my2DPosition, angleInDegrees: 45f, z: 1f);
}
}