Attack on Capsules (FPS Survival)

We've moved on from the SMHUP now that it's week four. Our next brief is a First Person Shooter survival. Personally, I've never done anything involving the Shooter genre in Unity, let alone a game. So this is quite new to me...

The base project we were given has a small platform with a wall, a barrel and another platform on a 40ish degree angle that's probably meant to look like a ramp. After creating a capsule enemy along with a spawner, we dived into Unity's NavMesh component.
From what I can tell, it's an AI pathfinding framework of some sorts. After "baking" the platform and telling the enemy to pathfind towards the player, that was it. The engine took care of the most efficient route to the destination without us having to write out own method/algorithm.

We added a line of sight requirement by raycasting constantly to the player's position (Since a raycast requires a direction, simply taking the target's position and subtracting your current position can give a normalized value which is the direction). If the raycast hit an obstacle between, the capsule would idle until it actively saw the player.
Screenshot of the scene with the Raycast in action. Since the capsule at the back doesn't have perfect line of sight of the player (blue capsule with a rocket launcher and a camera in it's head), it Idles.

The code that was used to construct the raycast

//Raycast towards the player
RaycastHit hit;  
if(Physics.Raycast(transform.position, (target.transform.position - transform.position).normalized, out hit)) {  
    //Hit player?
    if(hit.transform.tag == "Player") {
        //State transition to attack
        enemyState = EnemyState.Attack;
        Debug.DrawLine(transform.position, hit.point, Color.green);
    } else {
        //State transition to idle
        enemyState = EnemyState.Idle;
        Debug.DrawLine(transform.position, hit.point, Color.yellow);
    }
}

A feature of our implementation is that when the Enemy doesn't have line of sight but previously saw the player, it continues to move to the last known position of the player. This becomes a great feature for searching enemies, which can be expanded so they move to a known location and spread out from there.

The last edit we ended up making is having the enemy take damage from the rocket launcher but in a clean code approach. What that entailed was to make an Entity script which both the enemy and barrel would inherit from. From there the projectile's explosion radius just needed to call the TakeDamage function on the Entity script. The same thing could be applied to the player as well in the future.

Tom Lynn

Read more posts by this author.

Australia http://rubbix.net