Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

More constructors for Vector3 and Vector2?

Discussion in 'Scripting' started by TruffelsAndOranges, Jan 29, 2016.

  1. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    Hi! I am making a 2D game, and I constantly have to create Vector2s from Vector3s. It'd make my life so much easier if there was a constructor for Vector3 that looks like this: Vector3(Vector2 v) and a constructor for Vector2 that looks like this: Vector2(Vector3 v). And could then be used like this:

    Vector2 v2 = new Vector2(v3);
    Vector3 v3 = new Vector3(v2);​

    The third element in the Vector3 case should just be set to 0.0. Why hasn't this been added yet? Is there any real reason for not having such constructors?

    Such casts/constructors/operators already exists in the shader languages that are used in Unity, so why not just add them to the engine too?
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you can make some static class with a static function that takes the vector you want as overload and returns the other one you want.

    seems you can't inherit or extend a struct.

    or maybe you could write your own struct that has the functions you want and can be easily converted to a Vector3 or 2
     
    Last edited: Jan 29, 2016
  3. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    According to the documentation there are operators that allow implicit casts between the two. So you can just say v2 = v3 or v3 = v2. They're value types so you get a copy.
     
  4. DougMcFarlane

    DougMcFarlane

    Joined:
    Apr 25, 2009
    Posts:
    197
    @Dave Carlile is right. I did that recently for assigning v2 to v3, and v3 to v2.
     
  5. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    Ah. That's nice.
    Follow up question: why can't you send a Vector2 to a method/function as an argument that requires a Vector3 and vice versa? It'd make sense to me if the implicit cast was made in those cases too.
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    If you have a lot of stuff you're missing maybe make your own vector struct that can handle V2 and V3 the way you like it?
    i.e. using your struct and it's constructor could solve your issue with overload.
    Code (CSharp):
    1. public struct Vector
    2. {
    3.     public float x;
    4.     public float y;
    5.     public float z;
    6.  
    7.     public Vector(Vector2 _v2)
    8.     {
    9.         x = _v2.x;
    10.         y = _v2.y;
    11.         z = 0;
    12.     }
    13.     public Vector(Vector3 _v3)
    14.     {
    15.         x = _v3.x;
    16.         y = _v3.y;
    17.         z = _v3.z;
    18.     }
    19. }
    this way you could make you functions to ask for a vector so:

    Code (CSharp):
    1. DoSomething(new Vector(v3));
    2. DoSomething(new Vector(v2));
    3.  
    4. public void DoSomething (Vector v)
    5. {
    6.     //do stuff
    7. }
     
  7. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Hmm, I'm able to pass a Vector2 to a parameter that's Vector3 and vice versa. But not when the parameter is a ref parameter, which makes sense when passing a struct reference. Do you have some sample code that doesn't allow it?
     
  8. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    That doesn't work for built in functions and don't want to write wrappers for eveything!
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    These are both legal:
    Code (csharp):
    1. Vector2 v2 = new Vector3(x, y, z);
    2. Vector3 v3 = new Vector2(x, y);
    3.  


    This is because Vector2 has this implicit cast defined:

    Code (csharp):
    1. public static implicit operator Vector2(Vector3 vector3) {
    2.     return new Vector2(vector3.x, vector3.y);
    3. }
    Similarly in Vector3:
    Code (csharp):
    1. public static implicit operator Vector3(Vector2 vector2) {
    2.     return new Vector3(vector2.x, vector2.y, 0);
    3. }
    Which means that if you use Vector2 anywhere a Vector3 is expected, and a Vector3 anywhere a Vector2 is expected. Read up on implicit for a better explanation.