Search code examples
c#unity-game-enginedebuggingvector2

Float values get rounded creating a Hex Grid in Unity


My code generates a grid with a specified width and height and Instantiates a "hexTilePrefab" at the calculated floating point positions. The prefab has a class "HexTile" attached to it. Within HexTile "TileCoordinates" is defined as: public Vector2 TileCoordinates { get; set; } As seen in the comments of my code, assigning the directly by using a new Vector2() rounds the coordinates. As an example view image 2, where the coordinates are x=0.5 and y=0.86, but the code produces x=1 and y=1. Only if I assign the x and y values separately to a vector and then assign that vector to the coordinates, they are not rounded and are correctly established.

Screenshot

I would like to understand why this is the case, as I don't see how a Vector2 would automatically round any values. Thanks in advance!

I used the Console to keep track of the xPos and yPos values at all times and they always seemed to be correct. But right after tile.TileCoordinates = new Vector2(xPos, yPos); would be called and I would print out the TileCoordinates, they would suddenly be rounded.

I also asked ChatGPT 4o and didn't really get any further (to no surprise) :-)

Here is the code as text:

void GenerateBoard()
{
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            float xPos = x * tileOffsetX + (y % 2 == 0 ? 0 : tileOffsetX / 2);
            float yPos = y * tileOffsetY;
            
            GameObject hexTile = Instantiate(hexTilePrefab, new Vector2(xPos, yPos), Quaternion.identity);
            
            HexTile tile = hexTile.GetComponent<HexTile>();
            
            // xPos e.g. = 0.5 
            // yPos e.g. = 0.86
            
            // works, coordinates are not rounded
            var vector2 = tile.TileCoordinates;
            vector2.x = xPos;
            vector2.y = yPos;
            tile.TileCoordinates = vector2; 
            //-->                               tile.TileCoordinates = (0.5, 0.86)
            
            // doesn't work, coordinates are rounded
            tile.TileCoordinates = new Vector2(xPos, yPos);
            
            //-->                               tile.TileCoordinates = (1, 1)
            
            /*
                why???
                TileCoordinates is a Vector2, xPos and yPos are floats
                in HexTile defined as:
                public Vector2 TileCoordinates { get; set; }
            */                
        }
    }
}

Solution

  • Vector2 values do not round values. ToString of Vector2 does by default round to 2 decimal places, but this is only when printing them out. The actual values are not affected.

    Note that 0.86 is not the correct value for a hexagon. It should be sin(60) or sqrt(3f/4f) which is more like 0.8660254...

    Your two code snippets will result in the same value. It doesn't matter how you construct the Vector2 value. In the end it's just a struct / value type. The Vector2 constructor is literally just this.

    public Vector2(float x, float y)
    {
        this.x = x;
        this.y = y;
    }