Searching for good node grid building placement tutorials

I found a great tutorial series by Sebestian League that explains all the stuff about pathfinding which will be very helpful if I ever want to order units around and such on a grid. However it would be great if I could get a tutorial that specifically deals with with click and drag style mechanics and building placement on a similar sort of grid.

This is the tutorial series I found so far.

However as I said I’d love to get an in depth explanation of the code around click and drag building mechanics or just having the mouse highlighting various parts of the ground. I was thinking very much in terms of cities: skylines which actually uses this kind of system and is in Unity so I know it’s possible, I just want to find some good information on the techniques used here.

My problem with my current stuff is while I have had no problems getting stuff created from a button. It needs to be a lot more solid and I know I need a grid to do that and have everything calculated accurately. Right now when I click and place things it simply drops from the current mouse position after I place it when I want to be able to set up a grid and have the buildings automatically snap to the a grid on the plane I have.

1 Like

Bumping because this got to the third page without an answer, would appreciate some help on this one!

Hi Lethn, snapping positions is very easy:

  • Choose whatever is a relevant cell size for your setup, let’s say 1 unity distance unit.
  • Then you either use Mathf.Floor, Mathf.Round, or Mathf.Ceil (which ever gives best result for you, doesn’t matter which you use as long as you are consistent) on both axis from your plane.

If you’re making a city builder you’ll probably have a bunch of 2 arrays to store the various data layers so you might want to use Mathf.RoundToInt etc methods to get matching coordinates in those.

Do you have any form of full syntax though in C# for the actual grid itself? I just need something basic to work from then I can experiment and add things myself.

Having trouble locating good tutorials or information, when I put Unity grids in google it always ends up talking about the editor lol and yes, I did keep hearing about mathf but like I said I need a solid tutorial on all of this.

There are tons of tutorials on the A* Algorithm and pathfinding etc. but amazingly very few on building grids, no wonder there’s a short supply of proper city builder games to play :stuck_out_tongue:

Supposing you work in 3D, i guess you’re doing raycasts to find world position bellow mouse:

RaycastHit hit;

if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
{
     Vector3 worldPosition = hit.point;
....
}

From there, you have a position on your plane that isn’t snapped to your grid so you need to do what I said in my previous post:

     //this works for a cell size of 1 unit
     worldPosition.x = Mathf.Round(worldPosition.x);
     worldPosition.z = Mathf.Round(worldPosition.z);

And there you have a world position that snaps to your grid, you can use it to show a one cell sized quad for example, or position buildings etc…

Ah, sorry I should have been clearer, I was talking about the actual creation of thegrid, I’ve seen stuff like the node grid on sebestian league’s tutorials and then other things like creating planes and duplicating them. I also don’t know how to instantiate an object to the grid itself.

At least, I’m assuming I’d have to use instantiate in a situation like this, good explanation on the rounding though, I’ve seen mathf around a lot when it comes to these damn grids lol but there’s little information on the grids themselves and how you would create them and use them.

There are many ways to display a grid, some more complicated and some simpler.

If your map is small, start simple with a square texture that you repeat on a quad and simply instantiate objects on top of it at the snapped world position and you’ll get away with it.

If you’re aiming at bigger maps you need to do smarter things and if you have no idea of what you’re doing you should head to the asset store, there are great grid assets there.

If I was to do it, I would probably store data in 2d grids and only display what is in camera view, by moving arround pooled world tiles & objects (with lods to allow zooming in/out). With such method you can have grids up to 4k cells in only a few hours of work (which would take 64MB per layer of 4 bytes of data so not very mobile friendly imo, but acceptable if you aim at desktop/console or grids a bit smaller). But I think it heavily depends of what you plan to acheive…

1 Like

a “grid” is usually stored in a 2d array of tiles. If you are taking that approach you can just raycast to see which tile you are over and use it’s position to instantiate the gameobject at the centre of the tile.

without using a tile/array system you can instantiate a gameobject using the rounding approach above quite simply using the rounded position with some y value as the position.

1 Like

Okay! That’s starting to make more sense now, so what you’re saying is you’re using a mixture of raycast ( which will point the objects you intantiate to the points on the grid like with bullets in an FPS game except you use it for placing objects instead ) and then instantiate will be what creates the objects in the first place after you click a button.

Am I right in that line of thinking? Looks like I need to start looking at raycast tutorials.

By the way, what I’m planning on is making a space station management game, pretty similar to Startopia, it won’t be massive, but the grid itself will need to be pretty scalable. I’ve already worked through the tutorials for a platformer and will actually be releasing my game once I’ve finished all the animating and art so I want to start trying something a lot more ambitious.

Just found a good explanation on raycasting here for those that want more information on that.

Thanks again for the help, I think I’ll be able to start experimenting now.

a raycast gives you a reference to the thing you’ve hit. From that reference you can access the transform component and get it’s position, or use getcomponent and get anything on that gameobject.

you can also use the “hit” to get the point where the raycast hit the collider.

which you use is up to what you are doing. If you want to instantiate a bullet hole decal in a fps you would use the hit point location. If you want to instantiate a wall model at the centre of a tile, you would use the hit.transform.position to access the tile’s location etc.

Thanks, those explanations really helped, I know what to start looking at now, the thing with programming is everything has it’s own vocabulary so I just needed to find out the right damn commands/keywords to work with.

1 Like

This is a bit of a necro guys, but after some extensive searching I just came across this great looking set of tutorials on making square movement grids, this will help you get the code to set up the grids for raycasting and instatiation.

Just thought I’d post this in case anybody still was looking for information on the subject, it’s also in C# so no need to try and work out the right code from Javascript.

1 Like