How to insert a vector2 in a vector3?

How do I use the numbers from a vector2 array and use them in a vector3?

What I currently have is and change the vector2 components in the editor.

    public Vector2[] coordX;
    public Vector2[] coordY;
        Vector3[] verts = new Vector3[]{
            new Vector3 (coordX.x, coordX.y, 0f),
            new Vector3 (coordY.x, coordY.y, 0f),
            new Vector3 (coordX.x, coordX.y, l),
            new Vector3 (coordY.x, coordY.y, l)
        };

The errors I get are for all 4 vector3:

Type UnityEngine.Vector2[ ]' does not contain a definition for x’ and no extension method x' of type UnityEngine.Vector2[ ]’ could be found (are you missing a using directive or an assembly reference?)

Type UnityEngine.Vector2[ ]' does not contain a definition for y’ and no extension method y' of type UnityEngine.Vector2[ ]’ could be found (are you missing a using directive or an assembly reference?)

The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)’ has some invalid arguments

Argument #1' cannot convert object’ expression to type `float’

coordX is an array, which element are you trying to access?

coordX[0].x

the x attribute of the first element in coordX array. etc.

What I’m trying to do is for every vector 2 in the array and use them to make those vector3’s

Might be helpful if I add me start function

    void Start () {
   
        for (int lane = 0; lane < coordX.Length; lane++) {
            addLane();
        }


    }

Why you are getting the error:
You are defining an array of Vector2:

and after you are trying to get the x value of your array, which does not exist because it’s an array… You have to target an element inside the array like so:

Vector3 v = new Vector3(coordX[0].x, coordX[0].y, 0);

Are you trying to start from an array of vector2 and create an array of vector3 out of it?
Copy Vector2 array into Vector3 array…

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour 
{
    public Vector2[] startCoord;
    public Vector3[] endCoord;

    void Start () 
    {
        endCoord = Vector2ToVector3Array(startCoord);
    }

    Vector3[] Vector2ToVector3Array(Vector2[] startArray)
    {
        Vector3[] endArray = new Vector3[startArray.Length];

        for(int i = 0; i < startArray.Length; i++)
        {
            endArray[i] = Vector2ToVector3(startArray[i]);
        }
        return endArray;
    }

    Vector3 Vector2ToVector3(Vector2 v) 
    {
        Vector3 v3 = new Vector3(v.x, v.y, 0);
        return v3;
    }   
}

Why do you have a Vector2 at all? Cant you just start out with a Vector3 and not have this problem in the first place.

1 Like

@sluice has the answer for you and you should consider what @TJHeuvel-net is saying as well. But if you still ahve to convert Vector2 to Vector3 a lot in the future, this is where a little syntactic sugar of the extension method may help you.

using UnityEngine;

public static class VectorTools {
    public static Vector3 ToVector3(this Vector2 v) {
        return new Vector3(v.x, v.y);
    }

    public static Vector3[] ToVector3Array(this Vector2[] v) {
        Vector3[] v3a = new Vector3[v.Length];
        for (int i = 0; i < v.Length; i++) {
            v3a[i] = v[i].ToVector3();
        }
        return v3a;
    }
}

you can use that code to create the extension method than in the future you can Convert a Vector2 to a Vector3 like this

Vector2 myVec2 = new Vector2(15f, 20f);
Vector3 myVec3 = myVec2.ToVector3();
Vector2[] test1 = {Vector2.down, Vector2.left, Vector2.one, Vector2.right};
Vector3[] test2 = test1.ToVector3Array();

Also by the way, in the constructor of a Vector3 you can leave out the z coordinate and it will set it as zero

One liner with Linq (but you’d need to double check performance).

var coordXV3 = coordX.Select(c => new Vector3(c.x, c.y, 0)).ToArray();
var coordYV3 = coordY.Select(c => new Vector3(c.x, c.y, 0)).ToArray();

That will convert all of your Vector2s into an array of Vector3s, all with a zero Z-position.

@Dustin-Horne From my experience Linq mostly just generates a little more garbage to clean up than there is also the cast back to a array at the end.

So unless your data set is recursive or its happening in the update loop performance wise there is little reason no to use linq

This isn’t necessary; Unity already does it.

Vector2 v2 = new Vector2(1.0f, 2.0f);
Vector3 v3 = v2;

Also the other way works (Vector3 → Vector2; z is dropped).

–Eric

oh should have checked that, so it has a built in implicit cast?

–Eric

Yes, Linq will be slightly less efficient. I’m not sure if .ToArray() actually copies to an array or casts to an array. I’d have to check on that. Select is a pretty friendly Linq method. There are some you have to be careful of though, I believe GroupBy is a good example, as with these being value types you’ll actually end up boxing and unboxing.

Yes. There is an implicit cast both ways.

As a side note having an implicit cast both ways is technically incorrect. The cast from a Vector3 to a Vector2 looses data, so it should be explicit.

It means that this code fails:

Vector3 myVector3;
Vector2 myVector2;

...

var result = myVector2 + myVector3;

Which would have been perfectly legal if the casting from Vector3 to Vector2 was explicit instead of implicit.

Here is another example of how the duel implicit cast can cause trouble:

Vector3 original;
Vector2 newVector2;
Vector3 newVector3;

...

newVector2 = original;
newVector3 = newVector2;
Debug.Log(newVector3 == original);

// Output: false

None of this is a big deal. But its something to be aware of if you work with vectors of both sizes a lot.

yeah a cast makes sense, but you would think Vector3 to Vector2 would be explicit

1 Like