05 / Devlog, Sep 20 '23: enemy design
GitHub: https://github.com/nebulousmango/The-Body-Metallic
Document references:
- Game design doc: link
Enemy object
The enemy is a red sphere with a cylinder at its front to act as its eye (so that the player knows which direction it's pointing in). The parent object uses a larger sphere collider as a trigger, an Enemy script and a Nav Mesh Agent component. The Enemy Model object has an Animator component, which has animations for the orb's regular movement (to make it bob up and down as it floats), and the orb's death (the orb spins as it explodes). The sphere has a separate collider of its own, along with a kinematic rigidbody. The Fail script uses this second collider to check for contact between the orb and obstacles.
Enemy script
Some of the functions in the Enemy script:
- Follow, which sets the Nav Mesh Agent's destination to the player's current location.
- Die, which starts a couroutine. The coroutine plays the orb's Explode animation, plays an explosion sound effect, instantiates a particle system for the explosion, and then destroys the orb after five seconds.
- OnTriggerEnter, which plays both the orb and player's respective die functions when the orb's collider comes in contact with the player while they're alive.
Nav Mesh Surface
The Nav Mesh for the orb, built with a Nav Mesh Surface component that uses physics colliders for its geometry. To make this mesh, I arranged various cubes along the area I wanted the orb to be able to move in. The cubes all had box colliders and were set to the Orb layer, which is the only layer the Nav Mesh Surface is set to include. I turned these objects off after I baked my final Nav Mesh.
The cubes I baked into my Nav Mesh were set some distance above the ground, which gives the orb the appearance of floating above the player. During the obstacle course, the orb has to follow the player across two ramps, and then return to its normal height. This part of the Nav Mesh uses a cube set to a higher Y position. The enemy Nav Mesh Agent's step height was increased to the distance between the two cubes. During the game, the orb switches smoothly between the lower and upper heights.
Patrol state
While the player is still in the first room, the orb patrols the area on the other side of the door. I added some extra space to the left of the corridor in the second room for this.
When the player presses the first switch and the door opens, they'll be able to see the patrolling orb right in front of them. They'll see the orb start to follow them once they step into the second room, but they should have enough time to run into the corridor before the orb reaches them and explodes.
The Enemy script's Update function runs the Follow function once the player has entered the second room, and the Patrol function until then.
The Patrol function moves the orb between two Waypoint objects. The waypoints each use a collision trigger to check whether the orb has reached that position. When the orb reaches one waypoint, a bool check sets the orb's new destination to the other waypoint.
Follow state
To detect whether the player's entered the room, I added a cube with a collision trigger right outside the first door. When the player steps past the door into the second room, the orb changes from the Patrol to Follow state, and moves to the player's location. The Enemy Trigger script handles this with the b_EnteredRoom bool. The script also displays a little message at the bottom of the screen telling the player to run away.
Enemy explosion
The particle system that the orb uses when it explodes: this package was exported from Unity's particles project. The Enemy script instantiates this prefab at the orb's location in its Death coroutine.
The player has a separate function for when they die in the orb's explosion. This function freezes all the player's other functions as usual, but it uses a few different camera angles and shows the You Died overlay at a different stage (the player's Death animation plays, then the camera snaps to the ground after 2.5 seconds, then the retry menu loads after another 2.5 seconds).
Get The Body Metallic
The Body Metallic
Automaton troubles.
More posts
- 04 / Devlog, Sep 20 '23: player designSep 26, 2023
- 03 / Devlog, Sep 20 '23: level designSep 26, 2023
- 02 / ColophonJan 13, 2023
- 01 / Devlog, August 15 '21: bug fixesAug 14, 2021