Coin Toss Distraction System

Objective: Allow the player to toss a coin to distract guards.

“Huh? What was that noise?”

Our hero is pretty stealthy, but sometimes there are just too many guards in the area. Let’s give him the power of distraction!

We’ll grant our player a one-time ability to toss a coin. The noise of the coin will cause our well-meaning but underpaid night watchmen to investigate the area of the sound.

When the player clicks the right mouse button, if they have not yet used their coin toss ability, the coin will spawn at the point clicked and make a slight noise.

Our code will then find all the guards. Fir each of them, it will call their public Distract method, passing them the position where the coin landed.

The Distract method sets the guard’s _isDistracted flag to true and sets their new destination to the coin’s position.

With the _isDistracted flag true, the Update method will now call the DistractedMovement method instead of the normal WaypointMovement method.

The DistractedMovement method waits for the guard to get close to the coin, then brings them to a halt.

Now our hero can continue his heist!

As things are now, the guard will stare at the mysterious coin indefinitely. We could enhance our guard’s AI by letting them “remember” their destination before they were distracted, setting a distraction timer, and once the timer is up, having them resume their pre-distraction course.

An independent Unity Developer.