Creating Coin Distraction Feature in Unity (Part 3: Send AI to Coin)

Objective: Create a code to move the guards to the coin

First I assigned the tag “Guard” to each guard in the scene.

The idea is to create a function in the player script called “SendGuardToCoin”. This function receives as a parameter the position of the coin. We have to get all the guards and assign the new destination.

To get all guards we can use the function GameObject.FindGameObjectsWithTag(“Guard”). Then I used a foreach loop to iterate each guard, get the GuardAI script and call a new function called “MoveTo” that receives the position of the coin as a parameter.

In the GuardAI script I create the function MoveTo. This function sets the walk animator parameter as true to activate the walk animation and sets the nav mesh agent destination to the coin position.

And that’s all!!! Now the guards walk to the coin when it is tossed.

--

--

--

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hacktoberfest

What Are SKINZ?

How to Upload File to AWS S3 Bucket Laravel

How to Upload File to AWS S3 Bucket Laravel

Installing the Essentials on the Motherboard (QuIcK gUiDe #1)

Migrating app to Huawei AppGallery

Web Scraping For Good

ctf.hacker101 — Petshop Pro

OOP Design Patterns 9— Iterator Pattern

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rusben Guzman

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

More from Medium

How to Create A Loading Scene in Unity

Using the Unity Profiler

Creating Rewarded Video Ads in Unity

Daily Progression: Player Setup!