Under review

Determine whether NPC is near the Player in 2D

Oliver Marshall 9 months ago in Game Creator updated by Marti (Lead Developer) 9 months ago 7


Is there a way that I can tell whether an NPC is near a Player in a 2D environment?

I was hoping to use Perceptors, but I don't think they work in 2D (at least it doesn't work here). 

Is there another way?

I can see that there is a Distance condition in the Hub, but this doesn't appear to work with 2D either. 


Unity version:
Game Creator version:
Under review

Unfortunately Game Creator has very limited support for 2D. Despite this, I don't see a reason why the Distance Condition shouldn't work in 2D. Do you get an error message? Can you post what's happening?

There aren't any errors, at least not in the console. 

I have a condition assigned to a selector in a behaviour.

The behaviour is assigned to the NPC which, in my mind, should make the behaviour stop, and thus the NPC, when the player is near the NPC. However the NPC just keeps moving. In this image the NPC is the one moving, the player is the little chap that's standing still.

Although the node is a Sequence (not a Selector), the behavior should be different, as you mention. One question though, did you add the HookPlayer component to the Player? This is required so Game Creator knows which object is the Player.

If you've done this, drop a line and I will try to set up a similar scene and see what's happening.

Yes the Hook Player component is on my Player prefab.

Ultimately, I want to make the behaviour stop when the player approaches, so that the NPC can move around until the player comes close, then the NPC will either stand still or perhaps approach the player.

Strange, I've tested this and everything seems to be in order. The only thing that might not be working is the references to object A and object B in the Distance condition.

Could you try adding a sequence between the Entry and the Sequence node, adding a left-most Task that uses the "Debug Name" Action to print the names of both the Invoker and the Player?

This should help identify which objects the Behavior Graph is considering as Invoker and the Player. Maybe one of them is null and thus, making the Distance condition return false.

Thank for that. 

I've changed my behaviour nodes to show this:

When I play the game, console shows lots of this:

CHAR_NPC1 (1) is the name of the NPC object to which the behaviour is assigned.

PLAYER is the name of the my cunningly named player object.

I've tested this and everything seems to be fine. To narrow down the issue, I'm uploading here a new Condition package which works exactly the same way as the one you have but also prints information on screen. Would you mind installing this Action and posting back the results?