Toss a Coin: Creating a Simple Distraction System (Game Dev Day 62)

Jack Leavey
2 min readJan 17, 2022

With the guards pathfinding and patrol routes covered, I needed to give the player some way to alter the guards’ behavior so they can successfully sneak past them.

Enter the coin toss, a simple yet effective method of getting the guards out of their positions

Coin Toss Animation

So what is actually going on?

I am instantiating a coin prefab at the point a RayCast hits the floor. This is the same method used for movement, but I am using the RMB to perform this action.

You can see the coin being instantiated.

Logically, other than the instantiation, I set a bool to only allow this to be used once per play session.

The coroutine to redirect the guards uses a tag (Guard1) to locate each individual guard, then set their destination to the coin’s current location.

There is also a few lines to ensure that their animations do not break because of the sudden new command.

Finally, in the GuardAI script, I added another bool to make sure the guards continue their normal patrols UNLESS a coin is tossed.

All the patrol information is contained in this “if” statement.

--

--

Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!