For my current project, I have to create a physics engine, but with the restriction of not being able to Unity’s built-in Vector classes, at least for calculations. How would I go about creating a custom vector class?
public class Vector
{
float x, y, z;
}
And now the tricky part… Class or struct? ![]()
Conceptually a struct is probably better for a vector.
If you are making a Physics engine I suggest using struct as value types are cheaper to clean up as they are on the stack vs classes are reference types and are heap allocated.
Sorry! I should have clarified my question.
I do have the new Vector structure up, with the functions, etc. However, Unity doesn’t use it as just data storage, so I can’t use it unless I give it empty gameobjects ad infinitum. I can’t help but feel I’m missing something basic here.
Is there any way around that? Do I have to do something special with how I construct the class?
Just throw in an implicit cast back to a UnityEngine.Vector3 for applying to Transform.position.
You’ll probably want to duplicate most of UnityEngine.Vector3 methods and properties as well.
In fact you might as well just open UnityEngine.dll and copy out the Vector3 code into your own struct. You are pretty much going to do that anyway. There is really not that many ways to make a Vector3 class.
Array?
Unity should be fully able to use it to save value. Is your class/struct flagged [Serializable]?
[Serializable] did it. Thank you!