Triggering the NPC when we are close
Do you remember a game with an NPC in which, 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 we enter this radius, we will show the message, and if we leave, we will hide it.
The radius of influence will be a Collider. In Unity, Colliders emit a trigger when an object containing a Rigidbody component comes into contact; the same happens when the object leaves the Collider. We will use these events to create our dialog 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 Kaya@Happy Idle and select 3D Object | Cube, then rename it
Influence
. - Change the size of the cube to your own liking.
Figure 11.13 – Adding a cube to the scene
- Finally, with...