How to start detect key when enter a certain range and press to start dialogue

I want to make a rpg game and How to make the character enter a certain range of npc and then display a button or start to detect keyboard keys, and start a dialogue after pressing

So you want something like getting near the npc, pressing a button, and the dialogue sequence starts. Is that right? If so, my team and I are working on creating an RPG game like this.

If the player presses the button for interaction, it will cast a ray and check if it hits an npc. Then we check the distance between the player and the npc. The button will be ignored if they are too far from each other(since you want your game in 2d, I guess you should use something different from a raycast). If they are close enough, the npc will stop what it’s doing and enter a dialogue with the player.

We put up an asset for free for anyone who needs to solve problems like this. It has a fully functional dialogue system for talking with npcs. Example scenes are made in 3d, but if you know how to use Unity, it should be easy for you to use it for your 2d project. Here’s the link if you want to use it: Easy npcs | AI | Unity Asset Store