Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Follow publication

How to make a basic patrolling system in Unity/C#

Let’s see how to implement a basic waypoints-based patrolling system for a character unit!

Mina Pêcheux
Geek Culture
Published in
8 min readOct 17, 2021

--

🚀 Find all of my Unity tutorials on Github!

This tutorial is available either in video format or in text format — see below :)

Get the scripts for this tutorial on my Github :)

A bit of context

Let’s say that you just finished designing your level, and you’ve placed your enemies here and there, all fierce and mighty. Now, when your hero comes around, the enemies will go to him and try to attack — they will switch their state from an “idle” state to an “attack” state, for example, if you’re using a finite-state machine

But then, should the enemies just stay still when in “idle” mode? Wouldn’t it be better if they were patrolling back and forth in the level to keep an eye out for trouble?

Today, we’re going to work on implementing a simple patrolling system for an enemy, represented by a cube. The unit will walk steadily between a sequence of waypoints and, when it reaches the last one, it will loop back to the first one.

Preparing the scene

To begin with, let’s actually add a cube for our enemy in the scene. I’ll give it a basic red colour and place it at the origin point:

Then, I’ll prepare the waypoints to define its path. These waypoints will be empty game objects: this way, they will be invisible but we’ll be able to use them as target transforms for our enemy movement.

--

--

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 :)

No responses yet

Write a response