In-game Snap to Grid and different scales of objects

Hello,

Im writing RTS game. Im currently using this script for placing objects in grid:

 public static Vector3 MousePositionSnapToGrid(Vector3 position)
        {
            Vector3 currentPos = position;

            position.x = Mathf.Round(currentPos.x / ResourceManager.GridSpacing) * ResourceManager.GridSpacing;
            position.z = Mathf.Round(currentPos.z / ResourceManager.GridSpacing) * ResourceManager.GridSpacing;

            return position;

        }

It works great when building has all the same scales but it goes out of grid for building that has different scale. I think that it has something to do with pivot but im’not certainly sure how to fix this issue. Have You any ideas?

There’s screen describing the problem - Imgur: The magic of the Internet

As you can see building with scale 1 and road are placed correctly while building with scale 2 not.

Regards

I did exactly the same, my suggestion is that you put those buildings into an empty gameobject. Because it’s an child of that gameobject now, you can then position it so that a corner of the building is at the position of that gameobject. Then you just have to look that your building size can be divided by your grid size to make it align those grids. The main problem with your attempt is probably that you’re using the buildings center origin, if not my suggestion may be not working for you.