Week 4: Navmeshes and Simple Mechanics
"This is a quote. (Couldn't think of anything)"
~ Nathan Johns (1.08.2023)
Week 4: Navmeshes and Simple Mechanics
Most games nowadays have some form of NPC (non-player character) - these usually move around in the game world, sometimes providing challenge, and sometimes simply providing atmosphere. But how are these things done? Well, ignoring the harder aspect of AI, the actual decision-making, let's consider a simple game with enemies that approach the player. In this week we will go into navmeshes, what exactly they do, and how to use them with simple agents (a name for characters in the game). I'll then use this knowledge to create to create a simple game, with some mechanics like health and, well, that's about it. Anyway, let's go to it...
Navmeshing and Simple Agents
Now that we have a simple map to use for the creation of a game, we need to go over the basics of NPCs, and how they function in a typical 3D game. In most game engines, something known as a navmesh must be used - which is basically a machine-readable version of the map that only contains information about WHERE a character could feasibly wish to walk. Of course, a character can walk where a nevmesh has yet to be created, but without the navmesh to tell an NPC that they are allowed to walk there, they won't attempt to (unless specifically programmed to).
These navmeshes aren't too complicated - especially in Unity, which has its own built-in system for it. With the help of the 'AI Navigation' package for Unity, we can create a simple map for any terrain we please. All that needs to be done is simply specify what objects should count as walk-able, and what shouldn't (it's not THAT simple, but we aren't going into details). Once the objects that are walk-able have been listed, Unity can 'bake' a navmesh to be used for the current scene. This process takes some time for the initial time, but once it's finished, we end up with something like this...
With this navmesh created, and with the model I created in an earlier devlog, we are able to pretty simply create a following NPC. These character use a component that specifies them as a 'nevmesh agent', which has a few handy tunable settings to make them act more how you would wish for them.
With a little bit of coding, and some elbow grease, we can assemble a simple Unity script that lets us spawn these NPCS from their prefab on click. This creates a fun system to mess around with, but especially makes a good method of testing these 'agents' and their parameters.
Finally, we just need to make another script which gets the player's position, and sends it to the navmesh agent - this tells Unity where the AI wishes to walk to.
After a bit of adjusting, of specifying the character's radius, step-height, and speed; we eventually get what seems to be a nice little following character. They don't do much without a target, but once they sense a player in the scene, they will mark it as their target. With a target, these little guys just follow it around until the heat death of the universe, doing absolutely nothing else.
Yes, they're scary, but they are completely harmless (for now! [alluding to next chapter])...
Basic Game Mechanics
Having what we have, which is a Unity scene, with a navmesh, a controllable player and a following NPC; we may be able to make something that resembles an actual video game. Well, at the least, a playable game...
The most simplistic view of a playable game would be something that has a challenge for the player to overcome. In this case, we have something that could pretty feasibly be an enemy for the player to avoid, but we'll need some form of resource, or objective the player needs to achieve.
Adding in a simple health value to the player would add a resource for the player to preserve, having an amount of damage these 'enemies' could deal upon contact. We'll also need some way for the player to regain this health, so we'll add in some extra objects that the player can pick up - these will restore some of that precious life and allow the player to continue playing.
First, we'll go over the enemies. Notably, not much needed to be changed about these NPCs to create the functionality of a hostile force. We simply add on a collider to the character, functioning as a trigger area. With this trigger area, along with a script that runs events, we can detect any player entering the area, damage them, and destroy the NPC character.
Next is the health itself. Mostly, the health is just a script containing a value for the health, which can be a simple integer - in this case, we've defaulted it to a value of 10, which should be enough for the player to survive on. This script has two public functions that are accessible to the events provoked by enemies or pickups, allowing them to add or remove health on these collisions. Every time health is modified, this script will check if the player will be destroyed, and detaching the camera from their body (this isn't great for a finished game, but it will do for this test one - usually you would want a game-over message, or screen to display some statistics).
The final piece is the pickups, which funnily enough, work just like the enemies of this game. When the player enters a trigger radius around them, it will provoke an event - making the player health script add more to the health integer (not exceeding the maximum).
With these three simple pieces, the game is in a playable state. The goal isn't a complicated one, just avoid enemies and collect pickups - it is rather similiar to the game 'Snake', actually, although rather easier (and a lot more boring). Anyway, that's the basics needed to create a game - where to go from here to make it something fun, and unique?... That's the hard part.
References
UTAS KIT207 Portfolio
Status | Released |
Category | Assets |
Author | utas_nathan |
More posts
- Week 5: Lighting and ShadergraphsAug 14, 2023
- Week 4 (part 2): Game IdeasAug 06, 2023
- Week 3: Unity terrain and movementJul 31, 2023
- Week 2: Pain and SufferingJul 24, 2023
- Week 1: Blender BasicsJul 17, 2023