More constructors for Vector3 and Vector2?

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?

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

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.

@Dave-Carlile is right. I did that recently for assigning v2 to v3, and v3 to v2.

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.

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.

public struct Vector
{
    public float x;
    public float y;
    public float z;

    public Vector(Vector2 _v2)
    {
        x = _v2.x;
        y = _v2.y;
        z = 0;
    }
    public Vector(Vector3 _v3)
    {
        x = _v3.x;
        y = _v3.y;
        z = _v3.z;
    }
}

this way you could make you functions to ask for a vector so:

DoSomething(new Vector(v3));
DoSomething(new Vector(v2));

public void DoSomething (Vector v)
{
    //do stuff
}

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?

That doesn’t work for built in functions and don’t want to write wrappers for eveything!

These are both legal:

Vector2 v2 = new Vector3(x, y, z);
Vector3 v3 = new Vector2(x, y);

This is because Vector2 has this implicit cast defined:

public static implicit operator Vector2(Vector3 vector3) {
    return new Vector2(vector3.x, vector3.y);
}

Similarly in Vector3:

public static implicit operator Vector3(Vector2 vector2) {
    return new Vector3(vector2.x, vector2.y, 0);
}

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.