Vector3Int.Vector3 - how do I use this to convert a Vector3Int to a Vector3?

The documentation suggests this “Converts a Vector3Int to a Vector3”.

It’s listed under ‘operators’ (like +, -, !=, ==, etc) so I’m not entirely clear how to actually use it in code.

Any tips?

As an operator it would mean that it is an automatic cast.

Vector3Int vInt = Vector3Int.one;
Vector3 vFloat = vInt;

Int to Float is an easy conversion so they added it a an operator.

1 Like

Ah, so it just removes the need to explicitly cast it in that situation.

Excellent, thank you for the quick response!

Note that these are called type conversion operators. Those type of operators come in two different kinds: implicit or explicit. An implicit type conversion operator can, as the name suggests implcitly convert one type to another. This is generally inferred by the compiler. An explicit type conversion operator requires an explicit “cast”. It’s not a type cast in the classical sense but syntactically it looks the same. Every implicit cast can be done explicitly.

So the Vector3Int struct has two type conversion operators. One that converts a Vector3 into a Vector3Int implicitly and one that converts a Vector3Int into a Vector2Int explicitly. So as Chris said, you can implicitly assign a Vector3Int value to a Vector3 value and it will automatically convert the type. Though the conversion from a Vector3Int into a Vector2Int is explicit so you have to do it manually like this:

Vector2Int myVec2 = (Vector2Int)myVector;```

So the ```(Vector2Int)``` in the code above is not a "cast" but a type conversion. Casting only makes sense for reference types in C# (unless we talk about unsafe code and pointers).

Note that while the Vector3Int to Vector3 conversion is implicit, there's no implicit nor explicit conversion the other way since Vector3 values are floats and there are several ways how you may want to convert a Vector3 into a Vector3Int. That's why the Vector3Int class has 3 static conversion methods: FloorToInt, CeilToInt and RoundToInt. The [operators and those conversion methods are defined like this](https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector3Int.cs#L108-L150).
1 Like

Thanks @Bunny83 for that very thorough explanation (and for the link to the github code which made it even clearer!).

For context, the issue I was dealing with was the following:

Vector3 newPosition = worldPosition + myOffsetVector3Int * myDistanceFloat was resulting in the error:

Cannot apply operator ‘*’ to operands of type ‘UnityEngine.Vector3Int’ and ‘float’

So I was looking for options to convert myOffsetVector3Int to a Vector3 when I stumbled across the documentation I referenced in my original post. Turns out I can just do this:

Vector3 newPosition = worldPosition + (Vector3)myOffsetVector3Int * myDistanceFloat

I guess in this situation the compiler doesn’t know to implicitly convert myOffsetVector3Int to a Vector3 since just multiplying it by a float doesn’t give it any context, so I need to explicitly cast it in this case, yes?