Now it is time to give some intelligence to our NPCs. One of the scripts we will need to create is to give the NPC the ability to detect the player. This script will be called NPC_BarbarianMovement.cs. The script will be used to detect if the player is in-sight, calculate the field of view for the NPC, and calculate the path from the NPC to the player character.

Here is a listing of the source code:

using UnityEngine;using UnityEngine.AI;namespace com.noorcon.rpg2e{public class NPC_BarbarianMovement : MonoBehaviour{// reference to the animatorpublic Animator animator;// these variables are used for the speed// horizontal and vertical movement of the NPCpublic float speed = 0.0f;public float h = 0.0f;public float v = 0.0f;public bool ...

Get Building an RPG with Unity 2018 now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.