Hinged Door Script

What is going to happen in this script is when the player hits the door it will open.
When the player walks away the door will close.

Hi @Mason_Rocchio,

You can do so using triggers, that’s a volume that can fire an event when a body enters/exits it.

Check this tutorial on how to get started with that:


I got the door to rotate and move but I don’t know how to hinge it to an entity so it wont fall over.

So Ammo.js, the PlayCanvas physics engine, supports joints but they aren’t exposed in the engine to use directly.

There is this older example that you can take a look at, it uses legacy scripting, so be careful how you move that code over:


In general though for a door you may not have to use joints unless you are building something special. You can convert your door to a kinematic rigidbody and rotate it like a regular entity on the Y axis.

This way all other bodies can collide with it but the door won’t be affected by the gravity, fall from its position.

Oh ok

I will let you know if I still need help after this.

how would I add a hinge function in ammo.js

Check the example project I posted above, specifically the code in the hinge.js script:

I have hinge.js in my game

Well, the script is old using the legacy PlayCanvas script system.

But even if that wasn’t the case, you shouldn’t expect most scripts to work like this, copy/pasting in your project. You will need to study and understand how the code works in the scene context, and try to adapt it in your scene.

Try studying the PlayCanvas manual first, to understand how scripting works and also some JavaScript tutorials may help:


Ok thanks

Here’s an newer version of a project using physics constraints https://playcanvas.com/project/618829/overview/physics-constraints

It needs to be updated to use the engine’s joint system that has yet to be public API though

1 Like