LSL door script: touch to open, closes by itself
Free LSL door script for Second Life. Touch to open and close, the door swings on its hinge and closes by itself after a few seconds. Sounds optional.
A simple, reliable door script for Second Life. Touch the door and it swings open on its hinge; touch again to close it, or let it close by itself after a few seconds. It remembers where the closed door stands each time it opens, so you can move the door later and it still works.
// Touch Door with auto-close - free LSL script from WhatSL (https://whatsl.com/scripts/lsl-door-script)
// Free to use, change and put in your own creations, also ones you sell.
//
// Put this script in an upright door that is its own object (not linked to the house).
// The door's width must run along its X axis (the red arrow in the Edit window).
// Touch to open, touch again to close. It also closes by itself after AUTO_CLOSE seconds.
float OPEN_ANGLE = 90.0; // degrees to swing; use -90.0 to open the other way
float AUTO_CLOSE = 10.0; // seconds before it closes by itself; 0.0 = stay open
integer HINGE_SIDE = 1; // 1 = hinge on the +X edge, -1 = hinge on the -X edge
string OPEN_SOUND = ""; // optional: a sound name in the door's Contents, or a sound UUID
string CLOSE_SOUND = "";
integer gOpen = FALSE;
vector gClosedPos;
rotation gClosedRot;
openDoor()
{
// remember where the closed door is, so the door can be moved and still work
gClosedPos = llGetPos();
gClosedRot = llGetRot();
vector size = llGetScale();
vector hinge = gClosedPos + <HINGE_SIDE * size.x * 0.5, 0.0, 0.0> * gClosedRot;
rotation turn = llEuler2Rot(<0.0, 0.0, OPEN_ANGLE * DEG_TO_RAD>);
llSetLinkPrimitiveParamsFast(LINK_THIS, [
PRIM_POSITION, hinge + (gClosedPos - hinge) * turn,
PRIM_ROTATION, gClosedRot * turn
]);
if (OPEN_SOUND != "") llPlaySound(OPEN_SOUND, 1.0);
gOpen = TRUE;
if (AUTO_CLOSE > 0.0) llSetTimerEvent(AUTO_CLOSE);
}
closeDoor()
{
llSetTimerEvent(0.0);
llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_POSITION, gClosedPos, PRIM_ROTATION, gClosedRot]);
if (CLOSE_SOUND != "") llPlaySound(CLOSE_SOUND, 1.0);
gOpen = FALSE;
}
default
{
touch_start(integer total)
{
if (gOpen) closeDoor();
else openDoor();
}
timer()
{
closeDoor();
}
}
Free to use, change and put in your own creations, also ones you sell.
How to set it up
- Make or rez your door. It must stand upright and be its own object, not linked to the house.
- Check that the door's width runs along its X axis (the red arrow in the Edit window). If not, rotate the door in the build tools or swap the size values.
- Open the Content tab, click New Script, paste the script and save.
- Touch the door. If it opens the wrong way, set OPEN_ANGLE to -90.0. If it turns around the wrong edge, set HINGE_SIDE to -1.
Settings
- OPEN_ANGLE: how far the door swings, in degrees.
- AUTO_CLOSE: seconds before the door closes by itself; 0.0 keeps it open until touched.
- HINGE_SIDE: which vertical edge is the hinge.
- OPEN_SOUND / CLOSE_SOUND: the name of a sound in the door's Contents, or a sound UUID. Leave empty for silence.
Tips
- For double doors, put the script in both doors and set one to OPEN_ANGLE 90.0 and the other to -90.0.
- Doors that are part of a linked house need a different method. The easiest fix is to unlink the door and keep it as its own object.
Frequently asked questions
Why does my door turn around its middle?
The script rotates around one edge it works out from the door's X size. If the door turns around the middle, its width is not along the X axis. Rotate the door so the red arrow runs along its width.
Can I lock the door?
This version opens for everyone. For a locked door, add a check at the start of touch_start, for example: if (llDetectedKey(0) != llGetOwner()) return;
Does the script cause lag?
No. It only runs when someone touches the door and when the timer closes it.