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:
- Import the package into your project. It should appear under the Assets folder in the Project window.
- 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.
- 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.
- 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:
- Drag and drop a walkable surface from the Project folder into the Hierarchy.
- 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.
- It may take a few moments to bake; you can track its progress by the blue progress bar in the bottom right.
- 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:
- 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.
- To move around in the Animator window: Alt + click and drag. Scroll to zoom.
- 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.
- To make a transition, right-click on the starting state and select “Make Transition”. An arrow will appear; connect it to the end state.
- To learn more: Unity’s State Machine Transitions
Making the Rules:
- 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.
- 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.
- 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.
- To learn more: Unity’s Animation Parameters