Search Unity

Feature Request I want transform.position to be vector2 by default for 2d games, or be able to set it to be default

Discussion in 'Scripting' started by Upp000, May 31, 2023.

  1. Upp000

    Upp000

    Joined:
    Mar 4, 2021
    Posts:
    96
    Transform.position is vector3 by default, and we need to change it to vector2 if we want to do something like
    transform.position+vector2
    , which is unnecessary performance cost.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This would not improve performance. Having to perform an if check between Vector2 and Vector3 would cause CPU branching, which is far worse for performance than unnecessarily adding two floating point numbers.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,097
    @OP
    It is not. Unnecessary performance cost is me wasting time having to write this.

    2D does not really exist in Unity. It is a 3D engine from the ground up. You would gain literally nothing, but everybody would end up having two different transforms and every single thing in the world that relies on this would suddenly have to detect this, switch context, use everything differently.

    Because you seem to have developed bad habits regarding 2D coding, I advise you to adopt a series of micro functions that will make your life easier.
    Code (csharp):
    1. Vector2 to_v2(Vector3 v) => new Vector2(v.x, v.y);
    2. Vector3 to_v3(Vector2 v) => new Vector3(v.x, v.y, 0f);
    I promise these conversions will occur in around 3 nanoseconds. A nanosecond is one thousand-millionth of a second. And you will read your code much better, and it will work faster because you'll actually work with Vector2s, until the very last moment.

    You also get to be the boss of the actual plane you wish to work with:
    Code (csharp):
    1. Vector2 to_v2(Vector3 v) => new Vector2(v.x, v.z);
    2. Vector3 to_v3(Vector2 v) => new Vector3(v.x, 0f, v.y);
     
    Kurt-Dekker likes this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,130
    What you're describing is at most a micro-optimization that is so insignificant you won't actually see it, but if on the off chance you did see it you would likely be at an insane scale and have far bigger considerations like switching to DOTS.

    Isn't Unity already doing that with this code?
    Code (CSharp):
    1. [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
    2. public static implicit operator Vector2(Vector3 v)
    3. {
    4.     return new Vector2(v.x, v.y);
    5. }
    6.  
    7. [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
    8. public static implicit operator Vector3(Vector2 v)
    9. {
    10.     return new Vector3(v.x, v.y, 0);
    11. }
    https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector2.cs
     
    Last edited: May 31, 2023
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,097
    Sure. Let me remind you of my book that goes by name "The worst things ever implemented by Unity ever".
    Implicit lossy conversions are a big no-no in that book. I wish Unity didn't do that, then newbies would actually stick to good practices and maybe, just maybe, they would consistently produce a maintainable explicit code. Perchance.

    Also observe the second example that is readily required, and fully compatible with the first one (if named correctly), but incompatible with both implicit and explicit casting. Asking for swizzling would be too much, I have accepted that as a reality.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    While we are going over these implicit casts and lossy noodling of data...

    Just know that the above is gonna cause you no end of hassle if you use that value for scale.

    The proper way to set scales:

    https://forum.unity.com/threads/onpointerenter-loose-image.1152686/#post-7396073

    NEVER set scale (or any part of scale) to zero or negative.

    NEVER use Vector2 because that makes the third term (Z) zero.

    ALWAYS use Vector3, and make sure that the .z is either 1.0f, or else your non-zero scale.

    Similarly never set scale to Vector3.zero, especially in a UI, because this will cause layout divide-by-zeros and damage all your hierarchy.

    Always best to be explicit: don't forget any of the data that is transformed by your code.
     
    Ryiah and orionsyndrome like this.
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,097
    Check out the following code to see how I'm handling 2D when I care [link]
    And this is when I don't care much [link]

    I'm not saying that my style is the best or the most preferable, but you can maybe discern some patterns and adopt some good practices.