Interact with an entity when at a certain distance

I’d like to make an option to “press E” to pop up on a 2d screen when A: getting within a certain range of an entity as well as B: looking directly at the entity. Once this is done, I’d like to make the entity a child of the player’s camera, and when they press E again on a specific target entity, x happens.

The project is here: PlayCanvas | HTML5 Game Engine
The entity in question is named “pickableEnt”
The entity I would like for it to interact with (after picking it up) is named “targetEnt”

Hi @cyron! My example project below is probably not exactly what you are looking for, but maybe it can help you.

1 Like

That does help!
My only issue though is that I’m trying to make it so that it can only be triggered when you’re within a certain distance (it’s a first person project). Do you have any advice on this?

What do you have so far?

I use also a pickable distance in my example project. So if you are not close enough to the object you can not pick it up.


Oh, I see. I have a script like that but I have farClip instead of pickableDistance. I’ll change it and see if it works.

1 Like

The script doesn’t seem to be doing anything. Can you take a look at it and see if there’s anything I missed? It’s called “objectPicker”

You probably have to replace line 20 of your script with something like below.

if (event.key === pc.KEY_E) {

Just tried it, still has the same result.

At least we solved a future problem now. :grin:

You need to debug your script to see which part is running correctly and which part is not. You can add for example some console.log('check 1'); to do this. Open the console of your browser with F12 to see the logs.

The laptop I am using doesn’t have an F12, only F1 through F10, is there any alternative keybind?
Edit: Nevermind I looked it up and there’s some obscure way to do it.
Edit edit: Turns out that didn’t work.

CTRL + SHIFT + I should work too.

Do I use this in the editor or when the program is launched?

In the launch page of your project.

Right, so oddly that doesn’t seem to be working either, when I get back home, I’ll try it on my home computer.

Seems to work for me. Your checks on line 16 and 27 are executed correctly.

So when I did that, nothing happened, I went back and just to see what would happen, I changed pickableDistance on line 26 back to farClip, and it seems to work now. Only issue is, now there’s no maximum distance from where you can pick it up.

It works in my example project, so not sure why it doesn’t work in your project.

You can also check the distance after a result. Something like below.

var distance = this.entity.parent.getPosition().distance(result.point);

if (distance < 10) {
    // start dragging

There’s something that’s being missed. I’m just going to copy your script over and tweak it a little bit, if that’s alright.

1 Like

Make sure your script name is the same as the script name you use in the script.

That made it work, thanks!

1 Like