How to convert Vector3Int to Vector3?

The recent adding of the Tilemap in Unity has made me very happy and I’m trying to use it. It seems that they also added a new thing called Vector3Int. Basically the same as Vector3, but it uses integers. Now the only way to get a tilemap into a script is by using Vector3Int name; And I kinda need floats. There is the Vector3Int.Vector3 operator, which should convert the Vector3Int into a normal Vector3, but I’m not sure what to do to make it work. It says it’s an operator so it needs to be used in an if statement for example, right? Doing this doesn’t work:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class tryOutTileMap : MonoBehaviour {

  
    public Tilemap Test;
    public Vector3Int.Vector3 tileMapSize;
    public Vector3 cellSize;
  
    void Start () {
    tileMapSize.x = 10f;
    tileMapSize.y = 10f;
    cellSize.x = 1;
    cellSize.y = 1;
    }

It displays the Vector3 part in red.

you cant declare a variable that ways… but you will can use
public Vector3Int tileMapSize;

then fetch : tileMapSize.Vector3

1 Like

That doesn’t seem to work. I use tileMapSize.Vector3.x = 10f; and it just displays both Vector3 and x in red.

I dont have access to unity now, can you tell me what autocomplete suggest when you type:
Vector3Int.Vector3(

Are you using unityscript? It’s invalid code to go tileMapSize.Vector3.x = 10f; in C# assuming it’s an actual Vector3 type.

I script in C#. My goal is just to be able to set the Tilemap size to 10f in the x and y axis.

It suggests nothing.

So unity 2017 doesnt have Vector3Int! which version of unity are you on?

2017.2.0f3

Judging by the fact they have it listed as an operator I’m guessing you can just assign it. Haven’t tried though.

Vector3Int vi = Vector3Int.one;
Vector3 v = vi;

Thanks for your suggestion, although it seems, yet again not to be working:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class tryOutTileMap : MonoBehaviour {

   
    public Tilemap Test;
    Vector3Int vi = Vector3Int.one;
    Vector3 tileMapSize = vi;
    public Vector3 cellSize;
   
    void Start () {
    tileMapSize.x = 10f;
    tileMapSize.y = 10f;
    cellSize.x = 1;
    cellSize.y = 1;
    }
   
    // Update is called once per frame
    void Update () {
        Debug.Log (cellSize.y + cellSize.x);
    }
}

Error: Assets/tryOutTileMap.cs(11,24): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `tryOutTileMap.vi’

That pretty says it and doesn’t mean the suggestion of KelsoMRK is invalid. Put this in Start instead of as initializer:

Vector3 tileMapSize = vi;

Yeah you can’t use non-static assignments as field initializers :slight_smile:

Vector3Int vi = Vector3Int.one;
Vector3 v;

void Start()
{
    v = vi;
}

can you do a type cast :
Vector3Int v1= new Vector3Int(0,0,0);
Vector3 v2 = (Vector3)v1;

1 Like

The explicit cast shouldn’t be necessary given what the documentation is saying. I’m on 2017.1 so I can’t verify myself.

1 Like

Looks like you don’t need to cast for:
Vector3Int v1= new Vector3Int(0,0,0);
Vector3 v2 = v1;

Just if you are using some operation like:
VectorInt v1 = new Vector3Int(1,1,1);
Vector3 v2 = (Vector3)v1 / 2;

1 Like

If you came here you are looking for:

Vector3Int.FloorToInt(Vector3);
Vector3Int.RoundToInt(Vector3);
or
Vector3Int.CeilToInt(Vector3);

You’ve got it backwards. This converts a Vector3 to a Vector3Int whereas the OP wanted to go the other way.

1 Like

since you can do this ?
Vector3Int st = new Vector3Int();
Vector3 des = new Vector3(st.x, st.y, st.z);

Vector3 vec3 = (Vector3)vec3int; works for me