Crash 3D will have enemies roaming the entire map. They will randomly decide which routes to chose so the enemy cars will go different directions every time the game is played. This removes the predictability factor in the game.
There will be a single spawn position on the map where enemies will be spawned.
When starting the game there will be only 1 enemy car spawned on the map along with you. After a fixed amount of time OR after a certain score has been reached, more enemies will spawn.
When you crash into the enemy head-on, the game will be over. If you can mange to ram into the enemy car from behind, you will be able to kill the enemy and earn extra score. The enemy will then re-spawn faster than before.
So on a technical side, what happens when the player has rammed into the enemy?
- A collision detection script checks to see if the game object collided with is actually the player.
- Stop all movement scripts
- The renderer on the enemy is turned off so the player cannot see it.
- The Collider of the enemy is turned off to avoid any collision issues
- Explosion sound is played
- Particle system is instantiated to simulate an explosion
- Wait for the sound and instantiation to complete
- Wait for the specified time before it can re-spawn
- Set the position and rotation of the enemy to be equal to the re-spawn position
- Increase the speed
- Turn on the renderer
- Turn on the collider
- Enable the movement script
AI Behind the Enemies
Enemies have a movement script controlled by a speed variable. The movement script moves the enemy forward at the speed specified by the speed variable. It also allows the enemy to swerve left and right randomly without hitting other game objects (buildings, walls, etc). To make this possible, the enemy spawns exactly in the middle of the road. After a random amount of time, the enemy decides to swerve left or right for a random amount of meters.
To check whether to stop swerving, a Raycast is used on both sides of the enemy. If the Raycast hits anything that is closer than 2meters the enemy will stop swerving.
The enemy will also randomly decide to return to the middle of the road.
How does the enemy know when to turn?
A group of trigger boxes is placed everywhere the enemy can turn. A trigger box is added just before the turn telling the enemy whether it can turn left,right or continue straight. Another trigger box in the centre of the turn is used to trigger the actual turn. So when the enemy hits the first box, it knows where it can go. When it hits the second box, it randomly decides between the possible turns and performs that turn.
See the screen shots for demonstration.