Cristian Garcia
Cristian Garcia

Reputation: 9859

Vector2 cast to Vector3 in C#/Unity3D

I had some code like this

Vector3 someFun (Vector2 v) {

    return new Vector2 (...);
}

As you see the types dont match but there is no error. Does Vector2 inherit from Vector3 or is there a cast between the two?

Upvotes: 1

Views: 14303

Answers (2)

rhughes
rhughes

Reputation: 9583

You can pass the needed values in the Vector3 constructor:

Vector3 someFun (Vector2 v) {

    return new Vector3 (v.x, v.y, 0.0f);
}

Upvotes: 0

bubbinator
bubbinator

Reputation: 495

If I am understanding their documentation as found here, they use implicit operators to convert a Vector3 to a Vector2 and vice versa. I think that they are structs (for greater performance), which rules out inheritance.

Upvotes: 1

Related Questions