{"id":102,"date":"2026-11-10T20:27:09","date_gmt":"2026-11-10T20:27:09","guid":{"rendered":"https:\/\/courses.etc.cmu.edu\/53-353\/?p=102"},"modified":"2026-11-10T20:27:09","modified_gmt":"2026-11-10T20:27:09","slug":"lesson-10-fps-style-games","status":"publish","type":"post","link":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/2026\/11\/10\/lesson-10-fps-style-games\/","title":{"rendered":"Lesson 10: FPS-style Games"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This week we are looking at some advanced Unity features that we can use to create our own &#8220;first person shooter&#8221; (FPS) style game.  We will take a look at tools for quick level generation, dive into Unity&#8217;s Character Controller, take a deeper examination of the offerings of the Physics library, and finally dip our toes into some Enemy AI.  Starting today, our remaining lessons are no longer required to complete the assignments.  From this point on, all lessons are strictly educational, but I strongly recommend trying to follow along as these systems are very useful for developing your own games.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Part 1: Level Prototyping (with ProBuilder)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ProBuilder is a very useful plugin for Unity, extending the editor by offering simple (but significantly improved) modeling and alignment tools. &nbsp;Once its own standalone product in the Asset Store, Unity has purchased it and now offers it free in the package manager.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ProBuilder is helpful if you need to quickly prototype some objects, rough out a level design, or generate a quick mesh for collisions. &nbsp;This provides enhanced mesh generation, and access down to the vertex level, as well as a quick way to create and adjust UV mapping for objects. &nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another plugin from the same creators, PolyBrush, lets you sculpt, shape, and paint your meshes. &nbsp;It is also available for free in the Package Manager.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Installing ProBuilder<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">ProBuilder is available as a packet in the <strong>Package Manager<\/strong> (which you can access through the menus at <strong>Window &gt; Package Manager<\/strong>).   Select the <strong>Unity Registry<\/strong> option to see all available packages.   ProBuilder is available on its own, and also as part of the &#8220;3D World Building&#8221; bundle that also includes PolyBrush, FBX Exporter, and advanced Terrain Tools. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Select either the individual package or bundle and click <strong>&#8220;Install&#8221;<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the \u201cPackage Manager\u201d, accessed by clicking on&nbsp;<strong>Window &gt; Package Manager<\/strong><\/li>\n\n\n\n<li>Select the&nbsp;<strong>\u201cUnity Registry\u201d<\/strong>&nbsp;option to see all available packages<\/li>\n\n\n\n<li>Scroll down until you see the \u201cProBuilder\u201d entry, and then click&nbsp;<strong>\u201cInstall\u201d<\/strong>&nbsp;&nbsp;<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1006\" height=\"463\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-14.png\" alt=\"\" class=\"wp-image-428\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-14.png 1006w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-14-300x138.png 300w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-14-768x353.png 768w\" sizes=\"auto, (max-width: 1006px) 100vw, 1006px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">ProBuilder Interface<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To create a ProBuilder object, navigate the top menu to&nbsp;<strong>Tools &gt; ProBuilder &gt; Editors &gt; Create Shape<\/strong> and choose the type of shape you want to make. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will switch your context toolbar and add a modifier panel to your scene window:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"383\" height=\"152\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/11\/image-1.png\" alt=\"\" class=\"wp-image-552\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/11\/image-1.png 383w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/11\/image-1-300x119.png 300w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that when you access a ProBuilder object, you will also see small icons menu in your Scene toolbar that indicate the selection type that Probuilder will use: <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"127\" height=\"28\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/11\/image.png\" alt=\"\" class=\"wp-image-551\" style=\"width:261px;height:auto\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">These are, from left to right:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>VERTEX Selection<\/strong><\/li>\n\n\n\n<li><strong>EDGE Selection<\/strong><\/li>\n\n\n\n<li><strong>FACE Selection<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">By using these selectors, you can grab just a part of an object and use the various transform tools to deform them into the shapes that you want.  The ProBuilder menu contains a number of tools that can split, offset, subdivide, and transform these.   Additionally, you can use the &#8220;shift&#8221; key to generate new geometry, including Shift-Move (face) to Extrude and  Shift-Scale (face) to create an inset poly.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"677\" height=\"393\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-18.png\" alt=\"\" class=\"wp-image-432\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-18.png 677w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-18-300x174.png 300w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><figcaption class=\"wp-element-caption\">Shift-Move <strong>(Extrude)<\/strong> will generate new geometry along the offset<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"382\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-19.png\" alt=\"\" class=\"wp-image-433\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-19.png 713w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-19-300x161.png 300w\" sizes=\"auto, (max-width: 713px) 100vw, 713px\" \/><figcaption class=\"wp-element-caption\">Shift-Scale <strong>(Inset)<\/strong> will shrink the face and generate border polys<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, you can use Vertex Color to set the color tint of surfaces.  You can also set materials, and make adjustments to the UVW mapping (the way that texture images are applied to the surfaces.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"519\" height=\"308\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-20.png\" alt=\"\" class=\"wp-image-434\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-20.png 519w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-20-300x178.png 300w\" sizes=\"auto, (max-width: 519px) 100vw, 519px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">ProBuilder can also <strong>Export<\/strong> a model into common 3D formats, including OBJ and STL.  (I recommend using .obj as that tends to be the most common amongst other modeling applications).    <\/p>\n\n\n\n<p class=\"has-black-color has-luminous-vivid-amber-background-color has-text-color has-background wp-block-paragraph\"><strong>ADVICE:<\/strong>&nbsp;ProBuilder is great for quickly generating or editing assets, but is not recommended to be used throughout the entire process. The procedural generation of the mesh at runtime makes it susceptible to small errors, at which point your work may be lost without an option for recovery. Once you are happy with an asset, it is highly recommended that you run the&nbsp;<strong>EXPORT&nbsp;<\/strong>process to convert it into a mesh file in your Asset folder.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Resources<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.unity3d.com\/Packages\/com.unity.probuilder@5.0\/manual\/index.html\">ProBuilder Documentation (Unity)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Part 2: FPS Character Controller<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">CharacterController Component<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Unity has a number of standard assets available for use, including prebuilt first person and third person character systems, but for this class it is good to take a look at the methods that can be used to create your own, and for that we will use the <strong>CharacterController <\/strong>component.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The CharacterController looks very similar to the Capsule Collider, which is a reasonable shape for what is likely a humanoid character, but it behaves very differently.  First, there is no Rigidbody used in the collider, which means that we will not have momentum or transfer force if we should collider with a Rigidbody object.  The CharacterController moves along a provided vector, and uses colliders of the surroundings to inform or restrict its movement.  Among the useful aspects of the CharacterController, you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set a <strong>slope<\/strong> limit to prevent your player from climbing surfaces at a steep angle.<\/li>\n\n\n\n<li>Set a <strong>height offset<\/strong> limit to allow your player to overcome smaller changes in elevation like stairs.<\/li>\n\n\n\n<li>See whether or not the controller is on top of a collider using the <strong>isGrounded<\/strong> property.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The CharacterController moves you along using a heading or motion vector through one of two commands:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/CharacterController.SimpleMove.html\">CharacterController.SimpleMove( )<\/a><\/strong> will attempt move your character at a speed provided with a Vector 3, and only along the X-Z directions (any Y-axis velocity is ignored).  Gravity is automatically applied, making this method appropriate for games where player movement is primarily grounded or level, without jumping.<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/CharacterController.Move.html\">CharacterController.Move( )<\/a><\/strong> will attempt to move your character along a movement vector and will be affected by the colliders.  Movement occurs one axis at a time, and gravity is NOT included, so you must process and your own downward y-velocity as part of the Move command. <\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In our workshop, we modify the example script provided in the Scripting API for the <strong>Move( )<\/strong> command and include it in our player movement script.  In the Update( ) command we perform the following tasks:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Check to see if the player is grounded.  If so, we reset a y-velocity variable to zero, as we are not currently moving downward.<\/li>\n\n\n\n<li>Assemble a movement vector from the Input System&#8217;s <strong>Player\/Move<\/strong> values and the players <strong>transform.forward<\/strong> which returns a vector facing the +Z-axis of the object (this will be important later when we use look controls to rotate our player object).<\/li>\n\n\n\n<li>Check for a &#8220;Jump&#8221; command and if so, set the y-velocity to a value that will hit that height based on the current gravity settings.<\/li>\n\n\n\n<li>Process gravity by decrementing our y-velocity using gravity * <strong>Time.deltaTime<\/strong>. <\/li>\n\n\n\n<li>Combine the &#8220;move&#8221; and &#8220;y-velocity&#8221; into one movement vector.<\/li>\n\n\n\n<li>Check for a &#8220;Jump&#8221; command and if so, replace the y-velocity with an upward thrust.<\/li>\n\n\n\n<li>Process gravity by decrementing our y-velocity by our gravity value * <strong>Time.deltaTime<\/strong><\/li>\n\n\n\n<li>Use the <strong>CharacterController<\/strong>.<strong>Move( )<\/strong> command to move by that vector adjusted for <strong>speed * Time.deltaTime<\/strong>)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"> This gives us a method where our character can move forward and back, and strafe left to right, but only in relation to world coordinate.  In the next video, we use mouse input to adjust our heading and edit our movement vectors to correspond to the forward direction of our rotated player object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mouse Look<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In 1995, the Quake demo introduced &#8220;mouselook&#8221; as an option for rotating our player and gamers have never looked back. This method of using x-axis mouse movement to rotate the player heading, and y-axis mouse movement to tilt our camera up and down has become the defacto standard of FPS style games, and so we will build this here today.    Console players today will also be familiar with the &#8220;thumbstick&#8221; controls, where the left-thumbstick on a gamepad controls front-back and lateral movements, and the right-thumbstick controls rotation of the body and head tilt. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is important to think about  how we intend to structure this camera work, as we are not performing a free 6 DOF style navigation of a world &#8211; certain rules will apply.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>First<\/strong>, we will always remain standing upright and will rotate our entire body to turn, rather than our &#8220;head&#8221;.  This allows us to define our &#8220;body&#8221; as an object that is restricted to rotation around the positive-y or &#8220;up&#8221; axis, and we can turn as much as we want without restruction.  much as we want around the up-axis.   <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Second<\/strong>, our &#8220;head&#8221; should be at eye-level, and should only tilt up-and-down, not side-to-side.  This means that the world will always be rendered sky-up (we never see things upside down).  We can accomplish this by placing a camera as a child of the body object, facing forward (positive-z) and restrict movement to never exceed a rotation more than 90 degrees from 0 on the x-axis.   (Never more than straight up \/ straight down)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mouse\/Keyboard and Gamepad are two very different control systems, but thankfully the Input System helps work smoothly between both platforms.  Each already has a binding for the default Player\/Look action, and we can tap into this to read a Vector2 value.  But&#8230; there is one significant difference.  The gamepad thumbstick has a fixed range of motion, whereas the &#8220;mouselook&#8221; uses a &#8220;delta&#8221; value &#8211; the distance it has travelled since the last report.  This creates a mismatch between the two, with Gamepad Thumbstick limited to a vector magnitude of 1, while the mouselook is theoretically unlimited.   To correct this, we can add a &#8220;Processor&#8221; to our thumbstick&#8217;s binding, using the Scale Vector 2 option to adjust the read value.  It is not a perfect match, but you can get pretty close to parity here.  <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"397\" height=\"110\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/11\/image-2.png\" alt=\"\" class=\"wp-image-553\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/11\/image-2.png 397w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/11\/image-2-300x83.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Using the x value of the vector, we rotate the &#8220;body&#8221; object using the <strong>Transform.Rotate(<\/strong>Vector3 <strong>axis<\/strong>, float <strong>angle)<\/strong> command to rotate an adjusted amount around the Y-axis.  We then use the Y value to decrement the &#8220;head&#8221; rotation around its x-axis, updating a floating point value that represents the <strong>tiltRotation<\/strong> and clamping that rotation at +\/- 90 degrees.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Part 3: Physics<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Raycasting<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One of the basic functions for a physics engine is &#8220;raycasting&#8221; &#8211; calculating a ray along a heading from a point of origin, and continuing until it intersects with an object, and returning information about that point of contact. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This method will be particularly useful to us, as we will use it as a method of &#8220;shooting&#8221; our gun.  To do this, we will cast a ray originating from the position of our camera and have it move directly out to center of our view, along what is the &#8220;forward&#8221; (or positive-z) axis of the camera.  We use the <strong>Physics<\/strong> library to create our <strong>Ray<\/strong> and then <strong>Raycast( )<\/strong> it, reading the result.  We use the version of <strong>Raycast<\/strong> that takes both a Ray and a <strong>RaycastHit <\/strong>as arguments.  The <strong>RaycastHit<\/strong> uses an <strong>out<\/strong> method to return more than one parameter, and this result contains information about what the <strong>Ray  <\/strong>connected with, similar to the <strong>Collision<\/strong> type that we receive when a Rigidbody collides.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using this, we can test to see if the object we hit reacts to bullets, and in this case our barrel will.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"546\" height=\"352\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-21.png\" alt=\"\" class=\"wp-image-435\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-21.png 546w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-21-300x193.png 300w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><figcaption class=\"wp-element-caption\">Say your prayers, barrel!<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">OverlaySphere<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">What about explosions?  If our barrel is exploding, it should have an effect on the objects nearby that are capable of movement.   Thankfully, the <strong>Physics<\/strong> library has a function that allows us to quickly identify all nearby colliders &#8211; <strong>OverlapSphere<\/strong>.  This method allows us to designate a sphere by defining a center point and radius, and will return an array of all Colliders that are found partially or fully inside of that sphere.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using this array, we can then access each individual collider&#8217;s Rigidbody and apply an explosive force using <strong>AddExplosionForce( )<\/strong>, which is what we used during Astral Attackers to make our &#8220;dead enemy&#8221; blocks explode in a satisfying manner.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, since exploding barrels have consequences, we also check for objects in that collider array that also have the <strong>BarrelScript<\/strong> component, and if so we call a public function there to start a coroutine sequence that begins by smoldering and ends by exploding, allowing us to set off chain reactions.  Very cool! <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"819\" height=\"413\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-22.png\" alt=\"\" class=\"wp-image-436\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-22.png 819w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-22-300x151.png 300w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-22-768x387.png 768w\" sizes=\"auto, (max-width: 819px) 100vw, 819px\" \/><figcaption class=\"wp-element-caption\">I told you! <\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Part 4: The Navmesh<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">While it is fun to wander around blowing things up, it will be more fun if we add some enemy objects.  I want to create a zombie horde that has <em>some<\/em> intelligence and can pursue me through the level. In order to do this, I am going to use Unity&#8217;s <strong>AI Navigation<\/strong> package to give my zombie model some smarts.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"410\" height=\"321\" src=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-23.png\" alt=\"\" class=\"wp-image-437\" srcset=\"https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-23.png 410w, https:\/\/courses.etc.cmu.edu\/53-353\/wp-content\/uploads\/2024\/05\/image-23-300x235.png 300w\" sizes=\"auto, (max-width: 410px) 100vw, 410px\" \/><figcaption class=\"wp-element-caption\">There&#8217;s one now!<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In order to make this object \u201cintelligent\u201d, or at least smart enough to figure out how to navigate my level, I need to define the areas that it is capable of travelling. This involves generating a surface shape that is comprised of all of the valid positions that one of the intelligent agents could occupy, which we call a&nbsp;<strong>Navmesh<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To create a NavMesh, you must first install the <strong>AI Navigation <\/strong>package from the <strong>Package Manager<\/strong>.  Once you do this, you can add the <strong>NavMesh Surface<\/strong> component to your level object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Navmesh is created by using a voxel system to analyze the environment to determine what areas are safe for a <strong>Nav Mesh Agent <\/strong>to go.  Each &#8220;agent&#8221; has a profile &#8211; the default is &#8220;Humanoid&#8221; -that behaves in a similar manner to the <strong>CharacterController<\/strong> in that it defines a radius, height, slope limit, and step offset.   When we &#8220;bake&#8221; a NavMesh, the voxel analysis determines where this agent could travel on that surface and builds a simplified mesh object that will be the traveling surface.    Then, Agents are given a destination and will move according to their speed and rotation settings towards that location, following an optimized path along that mesh.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the past, navigable areas were designated by using a&nbsp;<strong>Navigation Static<\/strong>&nbsp;property, but this has been deprecated in newer versions.  Now the &#8220;bake&#8221; process will occur on objects with the modifier which indicates that this is an element to be included in the baking process.  You can include or exclude objects, and you can designate objects as obstacles &#8211; such as our barrels &#8211; that the agent will avoid but that will not affect the bake of navigable areas.  This is done using the <strong>NavMeshObstacle<\/strong> component. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To make my zombies work, I add a&nbsp;<strong>Navmesh Agent<\/strong>&nbsp;component to our enemy objects, and then a small script to tell that Agent component where to go. If we feed the agent a position, it will automatically move at the designated speed to to that point considering the shortest path and moving around obstacles and other agents.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">PlayerFPS.cs<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; quick-code: false; notranslate\" title=\"\">\nusing UnityEngine;\nusing UnityEngine.InputSystem;\n\npublic class PlayerFPS : MonoBehaviour\n{\n    public float playerSpeed = 5.0f;\n    public float jumpHeight = 1.5f;\n    public float gravityValue = -9.81f;\n\n    public float rotationSpeed = 1f;\n\n    public float pushPower = 2.2f;\n\n    public Transform playerCamera;\n\n    private CharacterController controller;\n    private Vector3 playerVelocity;\n    private bool groundedPlayer;\n    private float tiltRotation = 0f;\n\n    &#x5B;Header(&quot;Input Actions&quot;)]\n    public InputActionReference moveAction; \/\/ expects Vector2\n    public InputActionReference jumpAction; \/\/ expects Button\n    public InputActionReference lookAction; \/\/ expect Vector2\n    public InputActionReference attackAction; \/\/ expects Button\n\n\n    &#x5B;Header(&quot;Shooting Attributes&quot;)]\n    public float shotForce = 1f;\n    public AudioClip gunshotSound; \n    private AudioSource audioSource;\n\n    private void Start()\n    {\n        controller = gameObject.GetComponent&lt;CharacterController&gt;();\n        audioSource = GetComponent&lt;AudioSource&gt;();\n\n        \/\/ Lock the Cursor\n        Cursor.lockState = CursorLockMode.Locked;\n    }\n\n    private void OnEnable()\n    {\n        moveAction.action.Enable();\n        jumpAction.action.Enable();\n        lookAction.action.Enable();\n        attackAction.action.Enable();\n    }\n\n    private void OnDisable()\n    {\n        moveAction.action.Disable();\n        jumpAction.action.Disable();\n        lookAction.action.Disable();\n        attackAction.action.Disable();\n    }\n\n    void Update()\n    {\n        groundedPlayer = controller.isGrounded;\n        if (groundedPlayer &amp;&amp; playerVelocity.y &lt; 0)\n        {\n            playerVelocity.y = 0f;\n        }\n\n\n        \/\/ Rotate Body First \n        Vector2 look = lookAction.action.ReadValue&lt;Vector2&gt;();\n        transform.Rotate(Vector3.up, (look.x * rotationSpeed * Time.deltaTime));\n\n        \/\/ Rotate our Camera\n        \n        \/\/ update tilt rotation\n        tiltRotation += (-look.y * rotationSpeed * Time.deltaTime);\n\n        \/\/ check for bad values\n        tiltRotation = Mathf.Clamp(tiltRotation, -90f, 90f);\n\n        \/\/ set the rotation to this value\n        playerCamera.localEulerAngles = new Vector3 (tiltRotation, 0,0);\n\n        \/\/ where we are moving\n        Vector2 input = moveAction.action.ReadValue&lt;Vector2&gt;();\n        Vector3 move = (transform.right * input.x) + (transform.forward * input.y);\n\n        \/\/ Jump\n        if (jumpAction.action.triggered &amp;&amp; groundedPlayer)\n        {\n            playerVelocity.y = Mathf.Sqrt(jumpHeight * -2.0f * gravityValue);\n        }\n\n        \/\/ Apply gravity\n        playerVelocity.y += gravityValue * Time.deltaTime;\n\n        \/\/ Combine horizontal and vertical movement\n        Vector3 finalMove = (move * playerSpeed) + (playerVelocity.y * Vector3.up);\n        controller.Move(finalMove * Time.deltaTime);\n\n        \/\/ check to see if we fired\n        if (attackAction.action.triggered)\n        {\n            \/\/ define the ray\n            Ray ray = new Ray (playerCamera.position, playerCamera.forward);\n\n            \/\/ define the raycast hit (holds the information about what was hit) \n            RaycastHit hit; \n\n            \/\/ use physics to cast the ray\n            if (Physics.Raycast(ray, out hit))\n            {\n                \/\/ if this returned true, we hit something\n                Debug.Log(&quot;We hit this object: &quot; + hit.collider.name);\n\n                \/\/ make the gun sound\n                audioSource.PlayOneShot(gunshotSound);\n\n                \/\/ does this have a rigidbody? \n                if (hit.rigidbody)\n                {\n                    \/\/ apply a small force in the direction of the ray\n                    hit.rigidbody.AddForceAtPosition(playerCamera.forward * shotForce, hit.point, ForceMode.Impulse);\n                }\n\n                hit.collider.SendMessage(&quot;DamageHit&quot;, SendMessageOptions.DontRequireReceiver);\n            }\n        }\n    }\n\n    void OnControllerColliderHit(ControllerColliderHit hit)\n    {\n        \/\/ Rigidbody body = hit.collider.attachedRigidbody;\n\n        Rigidbody body = hit.rigidbody;\n\n        \/\/ no rigidbody\n        if (body == null || body.isKinematic)\n        {\n            return; \/\/ escape\n        }\n\n        \/\/ We dont want to push objects below us\n        if (hit.moveDirection.y &lt; -0.3)\n        {\n            return;\n        }\n\n        \/\/ Calculate push direction from move direction,\n        Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);\n\n        \/\/ Apply the push\n        body.AddForce(pushPower * pushDir);\n    }\n}\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">BarrelScript.cs<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; quick-code: false; notranslate\" title=\"\">\nusing System.Collections;\nusing System.Collections.Generic;\nusing System.Security.Cryptography;\nusing JetBrains.Annotations;\nusing UnityEngine;\n\npublic class BarrelScript : MonoBehaviour\n{\n    public GameObject explosionPrefab;\n    public GameObject smokePrefab;\n\n    \/\/ explosion parameters\n    public float forceRadius;\n    public float forceApplied;\n\n    public ForceMode forceMode;\n\n    private bool _barrelOnFire = false;\n\n    public void BarrelShot()\n    {\n        \/\/ the barrel is hit - explode the barrel\n        BarrelExplode();\n    }\n\n    public void BarrelBurned()\n    {\n        if (_barrelOnFire)\n        {\n            return;\n        } else\n        {\n            \/\/ now barrel is on fire\n            _barrelOnFire = true;\n\n            \/\/ run a coroutine to smoke\n            StartCoroutine(BarrelFuse());\n        }\n    }\n\n    private void BarrelExplode()\n    {\n        \/\/ make the explosion\n        GameObject explosion = Instantiate(explosionPrefab, transform.position, Quaternion.identity);\n\n        \/\/ queue the explosion for destruction\n        Destroy(explosion, 2.0f);\n\n        \/\/ destroy this object\n        Destroy(this.gameObject);\n\n        \/\/ Find all of the colliders within our radius\n        Collider&#x5B;] explodedColliders = Physics.OverlapSphere(transform.position, forceRadius);\n\n        foreach (Collider collider in explodedColliders)\n        {\n            \/\/ apply forces to rigidbodies\n            Rigidbody rb = collider.GetComponent&lt;Rigidbody&gt;();\n\n            if (rb)\n            {\n                \/\/ apply the explosive force\n                rb.AddExplosionForce(forceApplied, transform.position, forceRadius,10.0f, forceMode);\n            }\n\n            \/\/ look for barrelscripts\n            BarrelScript barrel = collider.GetComponent&lt;BarrelScript&gt;();\n            if (barrel) { barrel.BarrelBurned(); } \/\/ set it on fire, or cause it to explode\n        }\n\n    }\n\n    public IEnumerator BarrelFuse()\n    {\n        \/\/ start the smoke\n        Instantiate(smokePrefab, transform);\n\n        \/\/ wait for the fuse\n        yield return new WaitForSeconds(2.0f);\n\n        \/\/ time to die\n        BarrelExplode();\n    }\n}\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">EnemyChase.cs<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; quick-code: false; notranslate\" title=\"\">\nusing UnityEngine;\nusing UnityEngine.AI;\nusing System.Collections;\n\npublic class EnemyChase : MonoBehaviour\n{\n    private NavMeshAgent agent;\n    private Transform player;\n\n    void Start()\n    {\n        \/\/ get the nav mesh agent\n        agent = GetComponent&lt;NavMeshAgent&gt;();\n\n        player = GameObject.FindGameObjectWithTag(&quot;Player&quot;).transform;\n        StartCoroutine(GetRecentPosition());\n    }\n\n    private IEnumerator GetRecentPosition()\n    {\n        while (true)\n        {\n            agent.SetDestination(player.position);\n\n            yield return new WaitForSeconds(Random.Range(1f,2f));\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week we are looking at some advanced Unity features that we can use to create our own &#8220;first person shooter&#8221; (FPS) style game. We will take a look at tools for quick level generation, dive into Unity&#8217;s Character Controller, take a deeper examination of the offerings of the Physics library, and finally dip our [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-recap"],"blocksy_meta":"","featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"post_excerpt_stackable":"<p>This week we are looking at some advanced Unity features that we can use to create our own &#8220;first person shooter&#8221; (FPS) style game. We will take a look at tools for quick level generation, dive into Unity&#8217;s Character Controller, take a deeper examination of the offerings of the Physics library, and finally dip our toes into some Enemy AI. Starting today, our remaining lessons are no longer required to complete the assignments. From this point on, all lessons are strictly educational, but I strongly recommend trying to follow along as these systems are very useful for developing your own&hellip;<\/p>\n","category_list":"<a href=\"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/category\/recap\/\" rel=\"category tag\">Course Recap<\/a>","author_info":{"name":"Tom Corbett","url":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/author\/tcorbett\/"},"comments_num":"0 comments","_links":{"self":[{"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":6,"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions\/569"}],"wp:attachment":[{"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.etc.cmu.edu\/53-353\/index.php\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}