CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Follow publication

Creating a building grid-based placement system in Unity/C#

Let’s learn how to snap our buildings on a horizontal grid!

Mina Pêcheux
CodeX
Published in
13 min readJun 29, 2023

--

In a recent tutorial, I discussed how to implement a basic “free placement” building system, where we could just select a building type, and drag our mouse on the drag to place it wherever we wanted.

But, in some games, you don’t have that much freedom — your buildings are constrained to a specific grid.

So, today, let’s continue our exploration of building placement systems, and make a simple grid-based placement scheme. It will allow us to select a building type in the UI, point at a spot on the map and round this hit position to clamp it to the nearest cell. We’ll even have a nice grid display to help us visualise these cells:

This tutorial is available in video or text format — see below for the text version :)

Check out the Github repo with all my Unity tutorials! 🚀

To do all of this, we’ll start from the previous building placement system we prepared in our last tutorial, and we’ll just adapt it slightly to introduce this grid clamping. We’ll see that just by implementing a few extra chunks of logic, we can very easily convert this previous code into a building grid placer. So if you’re curious to get a detailed explanation of the core features of our building placement system, feel free to have a look at this tutorial :)

But anyway — with all that said, let’s dive in and see how to transform our free placement system into a grid-based one!

A quick sum-up

Just before we get to our grid placement system, let’s first do a quick review of our current free placement scheme, and of how we’ll have to update it to fit our needs. If you just want to get the assets ready for today’s…

--

--

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Mina Pêcheux
Mina Pêcheux

Written by Mina Pêcheux

I’m a freelance full-stack web & game developer. I’m passionate about topics like CGI, music, data science and more! Find me at: https://minapecheux.com :)

Responses (1)

Write a response