How To Create A Simple NPC

NPC – non-player character

There are several components required to create an NPC: an avatar, a surface on which to navigate, a set of animations, and rules that dictate when to play which animations.

Making the Avatar:

  1. Import the package into your project. It should appear under the Assets folder in the Project window.
  2. There should be an image of the animation’s avatar in the folder; drag and drop into the Hierarchy window to create a game object.
  3. If you click on the game object, there should be an animator script in the inspector. Click Add Component > NavMeshAgent. This tells Unity that this game object is a moving character that will navigate the scene.
  4. Click Add Component > Rigidbody. Any object with a Rigidbody will be subjected to physics in the Unity universe. This ensures that the NavMeshAgent will be able to interact with the ground (stand on it without falling through).

  Making the Surface:

  1. Drag and drop a walkable surface from the Project folder into the Hierarchy.
  2. From the Navigation tab, go to Bake > Advanced > Select “Height Mesh” > Bake. This ensures that the NavMeshAgent will actually walk on the surface; otherwise, it may appear to float instead. 
    1. It may take a few moments to bake; you can track its progress by the blue progress bar in the bottom right.
  3. Position the NavMeshAgent on or above the surface. If it’s working properly, the game object should fall and make contact with the surface without falling through. If it’s not, make sure that there’s a Rigidbody attached to the game object and that a NavMesh has been baked.

Making the Animations:

  1. Next to the Scene and Asset Store tab, there should be an Animator tab. Right-click in the Project window, click Create > Animator Controller. Three animation states should show up in the Animator window: Any State, Entry, and Exit. Attach the animator controller to your animator by clicking on the avatar in the Hierarchy and locating the “Animator” script; drag and drop your animator controller into the “Controller” variable.
    1. To move around in the Animator window: Alt + click and drag. Scroll to zoom.
  2. In your animation pack, there should be a set of animation states available for your animator. When your animation begins, there should be an initial state — designate this as your first animation. Drag and drop the animation into the Animator window; it should appear in orange, with an arrow leading to it from Entry. These arrows represent transitions between states.
    1. To make a transition, right-click on the starting state and select “Make Transition”. An arrow will appear; connect it to the end state.
    2. To learn more: Unity’s State Machine Transitions

Making the Rules:

  1. On the left side of the Animator window, there is a column designated for layers and parameters. Parameters are variables used to control transitions between states. Create a parameter by clicking on the “+” sign to the right, selecting the type, and setting a default value. These parameter values can be accessed and altered through scripts.
  2. To add a parameter value as a condition for entering a transition, first click on the desired transition. In the Inspector window, locate the “Conditions” section and add the necessary parameter values.
  3. Parameter values can be changed via scripts. Instantiate an Animator, use GetComponent to reference your animator, and reset values using SetBool or SetFloat, for instance.
    1. To learn more: Unity’s Animation Parameters

Setting a Destination:

  1. To set the NavMeshAgent’s destination: Unity’s NavMeshAgent.destination
    1. NavMeshAgents can “slide” across a surface; to fix this, you can add to your script: [NavMeshAgent variable name].velocity = Vector3.zero;
    2. To rotate the NavMeshAgent to face the player: Unity’s Transform.LookAt
Sign up for Initial Consultation

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation