Page 1 of 2

How to check if npc is in range of player's interaction range

Posted: Tue Jan 03, 2023 7:14 am
by Adeoon
I want to somehow check if npc is in player's range, what important my player can be in different scene than the npc. If npc is in players range then some button show above the npc which can start dialogue. I don't see any events that might help me with that. Is there a possibility to do that simple enough. I don't want to check conditions every update iteration etc.

Re: How to check if npc is in range of player's interaction range

Posted: Tue Jan 03, 2023 9:15 am
by Tony Li
Hi,

Can you use a Proximity Selector?

Re: How to check if npc is in range of player's interaction range

Posted: Thu Jan 05, 2023 7:21 am
by Adeoon
I don't really know how to do it. My case is a little bit different from proximity approach. As I said I want to setactive of the UI gameobject with image and button on it when player is in npc's range, but I don't see any range in proximity selector, since it works with triggers, I guess I should add collider for npc and player, but I am not sure. Can you explain me more

Re: How to check if npc is in range of player's interaction range

Posted: Thu Jan 05, 2023 9:17 am
by Tony Li
Hi,

Looking at the example image in the Proximity Selector manual page:

Image

you can see that the NPC has a sphere trigger collider that defines its interaction range. It also has a Usable component to tell the Dialogue System that it's detectable by Proximity Selector. When the player is inside the trigger collider, the Proximity Selector will detect the NPC. Please see the Interaction Tutorial video for more details about how to set it up.

Re: How to check if npc is in range of player's interaction range

Posted: Thu Jan 05, 2023 10:24 am
by Adeoon
I changed selector on proximity, but that approach doesnt work for some reason :/ I'll include player and npc objects with their components. I guess it should work, and my dialogue icon should be visible when player npc is in player's trigger range, and should be invisible when player is out of it. But nothing happends. What important I don't know if that might be the problem, but I use 3 dialogue triggers attached to same gameobjct. Two of them are enabling, disabling icon, and third one is for on use which starts conversation, this method is invoking when player clicks on the icon.

Re: How to check if npc is in range of player's interaction range

Posted: Thu Jan 05, 2023 12:02 pm
by Tony Li
If you want to use the Dialogue System's selector/usable system, it's much simpler than that. Where do you want the icon to appear? At a consistent screen position? (For example, in DemoScene1 the selector UI appears at the top of the screen.) Or above the NPC's head?

You don't have to use the Dialogue System's selector/usable system. You can use your own system instead. In that case, just configure your own system to call the Dialogue System Trigger's OnUse() method.

Re: How to check if npc is in range of player's interaction range

Posted: Mon Jan 09, 2023 3:25 am
by Adeoon
The icon should appear above the npc's head in world space. I don't really know what is wrong with that, I would like to use dialogue system for that if possible.

Re: How to check if npc is in range of player's interaction range

Posted: Mon Jan 09, 2023 4:28 am
by Adeoon
I am struggling with it, trying many different things and it still doesn't work :( So as I said my dialogues icons which on click should start each dialogue should appear above each npc when player is in his range, if player isn't then they should disappear or be hidden. That's the functionality I want to get. I hope you can help me!!

Re: How to check if npc is in range of player's interaction range

Posted: Mon Jan 09, 2023 5:49 am
by Adeoon
Also I guess standard ui selector elements is just one object, so when I have many npc's on my scene that probably won't help me. Also what's even more important, my npc's can be on different scenes while player is on different, I see that there is a warning that standard ui elements don't found on the scene.

Re: How to check if npc is in range of player's interaction range

Posted: Mon Jan 09, 2023 9:44 am
by Tony Li
As described in the Interaction Tutorial video, you can add a (world space) StandardUsableUI prefab to the NPC. When the ProximitySelector detects the NPC, it will show this UI instead of the standard screen space one. Here's an example scene (based on DemoScene1) that adds the Basic Standard Usable UI prefab to Private Hart. It also replaces the player's Selector with a ProximitySelector.

DS_TestProximitySelectorUsableUI_2023-01-09.unitypackage

Note: If you don't want to work with Proximity Selectors and Usable UIs, you can always use your own interaction system and configure it to call the NPC's DialogueSystemTrigger.OnUse(). The whole Selector/Proximity Selector thing is entirely optional.