[SOLVED] Door script does not work unless a specific line is commented

I’m working on a script that should open/close my door in a PlayCanvas project.

When I comment out this part of the code:

// var player = this.app.root.findByName(‘Player’);
// if (player.getPosition().distance(this.entity.getPosition()) < 4) {
this.toggleDoor();
// }

it works. But if I leave it uncommented, it doesn’t work — even when the player is very close to the door.

Why is this happening, and what is the correct way to check the distance between the player and the door?

Editor link: https://playcanvas.com/editor/scene/2322303

Code Editor link: https://playcanvas.com/editor/code/1394660?tabs=255871351,253118097&focused=255871351

P.S: the script name is doorInteractive.js

Hi @GAMEPIX!

Your code looks correct to me. I guess your ‘Player’ entity is not moving, because the script and components are on a child entity.

If you use ‘Character Controller’ instead of ‘Player’ for findByName() it probably works.

but the problem is in the entity door in entity neighbor house

You are checking the distance to an entity that is never moving, so the distance to that entity will always be the same. Is that correct?

nope, you can controll player with keys wasd

Here is maybe an issue

You can probably also rename ‘Character Controller’ to ‘Player’ and remove it from the current parent. (Make sure you keep one entity with the name ‘Player’ in your scene).

1 Like

Oh, it works, thanks

1 Like