top of page
Search
  • Writer's pictureAmit Prakash

Chasing is Fun - Basic AI


Chasing the player is very common in most of the Games - especially RPG games. The basic idea of the Chasing AI is to chase the particular character when it comes in radius of the character who's reponsiblity is to chase and damage the player. This could be the basic AI for AI system. This was my first task in this semester to implement it.

There are various ways to implement this. One way is to find the position of the player and look for the threshold range, If the player is in the range, enemy/NPC/monster can start walking/running towards the player. The only complicated part is to calculate the shortest path in the environment (which can also be implemented using A* algorithm).

Unity in-built navigation system solves this job very efficiently. All we have to do is to add Navigation Component to the NPCs/Monsters/Enemies.

Apart from adding component, we need to tell the Unity system about the scene containing obstacle which needs to be considered while calculating the navigation Mesh. Navigation Mesh in Unity are pre-baked (pre-determined) information about the structure of the scene environment. It mainly helps Unity to calculate various possible path avoiding all obstacle so that any Nav Mesh Agents can go to different location in the scene avoiding the NavMesh Obstacle. Nav Mesh Obstacle are also Unity components which needs to be added a Unity Object if they needs to be avoided in the path tracing.

After adding these components to the relevant Unity gameobjects, all we need to do is that to bake the Navigation Mesh for the scene. It's a good idea to make game Objects (which won't move at all) as static in Navigation Mesh. This helps Unity to pre-calculate things which makes the game run faster.

Only thing left is to Bake the scene to create the Navigation System. Another thing required in this implementation is that the Nav Mesh Agents need some sort of command on a particular condition which will intiate the chase towards the player. In this implementation, we are using distance based trigger, which I set in a C# script.

void Update () { float distance = (transform.position - player.transform.position).magnitude; if (distance <= distanceToPlayer) { GetComponent<NavMeshAgent> () .SetDestination (player.transform.position); if(distance < GetComponent<NavMeshAgent>().stoppingDistance) { transform.LookAt(player.transform); combat.Animate(); } } else { GetComponent<NavMeshAgent> ().SetDestination (startPosition); } }

Below is the small output of the basic AI System.


162 views0 comments

Recent Posts

See All

Game Engineering Blog Posts

Visual Studio Solution Setup and Asset Build System Lua Integration with C++, OpenGL and DirectX Difference Index/Vertex Buffer and Cross...

bottom of page