top of page

Day 18: Adding an enemy

Updated: Jul 24, 2023

My plan for the day:

I intend to work on adding an enemy AI that uses a navmesh to have a patrol and chase state. What I accomplished?

To begin with, I imported in an enemy model from Mixamo without any animations. Once that was done, I started working on a script that would add in serialized fields to add in markers, or "waypoints" where the enemy would patrol. I added in a field of vision into the enemy AI, which using raycasts would be able to detect whether "Player" was caught in between the sights of the enemy. If the player was seen, the enemy would stop patrolling, and change states to chasing. This would be done by saving the player's last known position every frame, and as such the AI would be able to use the baked nav mesh of the terrain to go to the last known position of the player. If the player ran around a few obstacles here and there, the player will eventually lose the enemy, and the enemy AI after waiting for a few seconds, will return to its last waypoint, going back to the patrol state.


_____________________________________________________________________________________________________


https://clipchamp.com/watch/NmphqQT6j34 (Link to a video showcasing the enemy AI)


What is a serialized field?

The [SerializeField] attribute is used to mark non-public fields as serializable: so that Unity can save and load those values (all public fields are serialized by default) even though they are not public.


What is a raycast?

Raycast in unity is like a laser beam, which starts from its origin, moves ahead in its specified direction, and goes to its max distance. Raycast will return true if it gets hit with any object else false. It is very useful for detecting objects.


What is baking?

Unity performs the calculations for Baked Lights in the Unity Editor, and saves the results to disk as lighting data. This process is called baking. At runtime, Unity loads the baked lighting data, and uses it to light the Scene.


What is a navigation mesh?

A NavMesh is a designated mesh in your Unity scene, which specifies navigable areas in your environment, including areas where characters can walk, as well as obstacles. This is useful for scenarios which incorporate pathfinding and AI-controlled navigation.



ree

Image Source: My Unity game

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page