From the course: Cert Prep: Unity Certified Associate Game Developer Scripting with C#

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Creating the Attack state

Creating the Attack state

- [Instructor] In the proceeding movie we created the chase state for our NPC character. This happens when the NPC can see the player, and so they decide to continually chase the player. But now of course, when the NPC gets near enough within the attacking range to the player the NPC should start attacking the player. Let's set that up in code, To do that, I'm going to go to the scripts folder and double click on AI chase to bring that into view here and inside the chase function we're going to establish an attacking distance. To do that I'm going to choose float attack distance and maybe it's going to equal the value of maybe two meters. When we are within two meters of the player character we can start attacking the player. To do this inside the wild loop here, I'm going to check what the distance is between the NPC and the player character. We've seen how to do that in the patrol state I'm going to use a Vector3.Distance…

Contents