Triggering when we are close
Can you think of a game with an NPC where when you approach them, the typical text Press the “E” key to interact appears on the screen? That’s exactly what we’re going to program. We will create a radius of influence for the NPC. When the player enters this radius, we will show the message; when they leave it, we will hide the message.
The radius of influence will be a Collider. In Unity, Colliders emit a trigger when an object containing a Rigidbody component comes into contact with them. The same happens when the object leaves the Collider. We will use these events to create our dialogue flow:
- To create our area of influence, we will first add a Collider. To do this, right-click on the GameObject of our NPC called Peasant Girl@Dwarf Idle and select 3D Object | Cube. Rename it
Influence
.
Figure 7.16 – Placing a cube on the scene
- Use the Scale and Move tools to zoom and...