3 Adding enemies and projectiles to the 3D game

 

This chapter covers

  • Taking aim and firing, both for the player and for enemies
  • Detecting and responding to hits
  • Making enemies that wander around
  • Spawning new objects in the scene

The movement demo from the previous chapter was pretty cool but still not really a game. Let’s turn that movement demo into a first-person shooter. If you think about what else we need now, it boils down to the ability to shoot and having things to shoot at.

First, we’re going to write scripts that enable the player to shoot objects in the scene. Then, we’re going to build enemies to populate the scene, including code to both wander around aimlessly and react to being hit. Finally, we’re going to enable the enemies to fight back, emitting fireballs at the player. None of the scripts from chapter 2 need to change; instead, we’ll add scripts to the project—scripts that handle the additional features.

3.1 Shooting via raycasts

3.1.1 What is raycasting?

3.1.2 Using the ScreenPointToRay command for shooting

3.1.3 Adding visual indicators for aiming and hits

3.2 Scripting reactive targets

3.2.1 Determining what was hit

3.2.2 Alerting the target that it was hit

3.3 Basic wandering AI

3.3.1 Diagramming how basic AI works

3.3.2 “Seeing” obstacles with a raycast

3.3.3 Tracking the character’s state

3.4 Spawning enemy prefabs