Search code examples
unity-game-engineunity3d-2dtools

Vector2D or Vector3D in 2D games


Is there any downside of using a Vector2D in a 2D game in Unity3d? Or should I always use a Vector3D? They seem to be interchangeable.


Solution

  • Just use Vector3 in 2d games. All the positions of objects are still plotted in 3 dimensional space so if you use Vector2D there will just be an unnecessary cast from Vector2D to Vector3Dand the third dimension will just be set to 0. Vector3D's z property is still taken into account for sprites on the same sorting layer and with the same 'order in layer' so watch out for that. For the most part the order that sprites are drawn in should be taken care of by their sorting layer and order in layer properties though.

    There are quite a few uses of Vector2D in the 2D physics engine but you don't really have to worry about it. If you pass a Vector3D then it will be implicitly converted and the z dimension will be discarded.