I did this using Unity's in built navigation tool. It allows the user to set out paths that the enemy can walk along and follow the player. I had spent a long time researching the topic and finding out the best way to go about completing this task. Having never undertaken a task like this before, I dedicated a lot of hours into learning how to code this set up and it has now paid off. Some basic code that i have written for the enemy movement is below.
#pragma strict
var agent: NavMeshAgent;
function Start ()
{
agent.destination = transform.position;
}
function Update ()
{
CheckLocation();
}
function CheckLocation()
{
agent.destination = transform.position;
}
What this does is check where the navigation agent is (the enemy) and transform his position to the agents destination (the player). This allows the enemy to follow the player, although only on the highlighted routes. If the enemy reaches an area they can't get to, they will find an alternate path.
Firstly I tested out the navigation routes in a simple test scene. As you can see below, the blue highlighted areas are where the enemy will walk and follow the player.
Within Unity the user is able to make certain parts of the level not walkable for the enemy. This will be key during gameplay and will mean we can give the player certain safety areas within a level. All the navigation routes are able to be tweaked, meaning each level that we design will need special consideration for the enemy movement.
I then went one step further and tried to get the enemy to jump over gaps and obstacles. I was eventually able to do this after a few hours of testing and trailing. Below is how the final areas look that I have applied the jump navigation to. Notice the arrows which show where the enemy will start and land.
So, once I had completed all of this work I then went on to add a navigation route onto the game itself. I started with the base of level one, and added a mesh over the scene so that the enemy would follow the player whilst avoiding all of the obstacles. This took a while as it needed to be tweaked multiple times to get it right.
After I had done this, I then went and added a navigation mesh onto the easy route of level one itself. This, again, needed a bit of tweaking but I managed to get it into place. Coding was added to both scenes to make the enemy move. These scenes now need multiple play tests to see what changes, if any, need to be made.
The next stage for me will be to add navigation routes onto the other difficulty routes within level one and then play test them.
Written by Phil
Good work phil. You and Tom are making really good progress on the game. I really like the fact that you are pushing yourselves continually to do new work with the dissertation, this is exactly what you should be doing.
ReplyDeleterob