As we continue to layer more and more complexity with game mechanics this will invariably lead to submechanics of larger more complex system. One such submechanic would be a distraction system as a part of a larger detection mechanic. We’ll be using a previous example of the waypoint system to better show this in action by simulating a bait or coin toss. When the player right clicks near an enemy, it will cause the enemy to stop what it’s doing and move to that spot for a moment before resuming it’s course. This can be adapted to a variety of situations and may require some tinkering on your end to get working for your use case.
Let’s start by creating the distraction object itself. This will be a primitive Sphere that we’ll color Blue to help make it pop.
Let’s go ahead and call it Bait.
Create a script on it called the same. This will be the brains of our distraction object, it will tell the cube when to begin it’s distracted sequence.
Next create a tag named “Enemy” and assign it to the cube (in this example) so that we can find it in the Bait code.
We’ll add a collider and a rigidbody (don’t forget to disable gravity) to the Cube so that it will be detected by the bait’s radius to help activate the distraction logic on the Cube that we’ll show next.
Blow up the sphere collider to 5 or so this will show the radius of the bait to draw in the Cube and tick IsTrigger.
Here is the Bait code, it’s pretty straight forward. Look for a enemy tag when an object enters the area then if it checks out, call the script controlling the enemy and enable the distraction method passing in the Bait’s transform so the enemy can locate it. Then go ahead and disable the sphere collider on the Bait to keep it from accidentally going off again.
Lastly, this is our Patrol script. This controls the enemy as well as help spawn our bait prefab when we click on the scene. It’s pretty straight forward. So now we’ll click somewhere in the scene and show the enemy leave it’s established patrol path to the bait, wait a few seconds, and the return to the path.
Here it is in action, when the enemy moves along the path and then the player clicks an area, it drops everything and moves directly to the dropped bait. After a while it will return to the pre-determined waypoint. This is a great jumping off point for further functionality, such as creating a suspicion routine if for example a player enters range for a few seconds or starting an investigation waypoint pattern. The possibilities are only limited by your imagination. Until next time, happy coding.