Creating a clickable NPC

Assuming you've created an NPC following the steps of one of the previous tutorials, it's simple to turn them into a clickable, interactive element of your game.

IMPORTANT:
• For 3D Model-based NPCs, you need to work on the root gameObject.
• For 2D Sprite-based NPCs, you need to work on the sprite child.

Begin by checking that the object mentioned above is placed on the Default layer (or whichever your Hotspot Layer name is defined as in the Settings Manager). Note that only this object need be on this layer: any others can be placed on the Ignore Raycast layer.

Then add the following two script components:

• Highlight
• Hotspot

The Hotspot Inspector is where you define your NPC's interactions:

If you are making a 3D NPC (i.e. one that makes use of either the Legacy or Mecanim system), you can make them glow when made active by the player's cursor. Once they have the Highlight script attached, you can drag themselves into the Object to highlight field.

If you wish your Player character to move towards the NPC before interacting with him, you can define a Walk-to marker that the Player will move to. In the Scene Manager, click Marker and position the new Marker to an appropriate place - usually just in front of the NPC.

Then assign this Marker to the Walk-to marker field in the Hotspot Inspector.

You can now define Interactions in your Hotspot Inspector. Each Interaction is a way for the Player to interact with the NPC. The exact GUI may differ slightly depending on your choice of Interaction method in the Settings Manager. Click the + button to create a new Interaction:

From here, you can choose what kind of Interaction it is. You can define what choices are available from the Cursor Manager. The Player action popup box determines what the Player character does before the Interaction occurs.

The Interaction itself is defined in a separate Interaction prefab, and referred to by the Interaction field. If the field is blank, you can click the Auto-create button to quickly create and refer to a new Interaction prefab.

IMPORTANT: Unity 2D NPCs

For NPCs made for Unity 2D mode, a couple of extra steps are required. Firstly, a Box Collider 2D must be added to the Sprite child. This will define the area over which the cursor can interact with the Character. Check Is Trigger? and position it so that it covers the whole sprite. Note that the Circle Collider, which is at the NPCs feet, should still be present.

Finally, make sure that a Rigidbody 2D component is added to the root object. Without it, the new Box Collider will rotate with the NPC, causing the clickable area to be invisible when viewed sideways.

Adventure Creator and this website are copyright Chris Burton, ICEBOX Studios

Go to top