Monday 14 May 2007

Teleport script

Over the months I have had sever problems with teleports in Second Life.

How do teleports work in SL? They all use a hack; when you sit, the object you sit on has information about your sitting position including the offset from the object center and rotation. This is to allow for poses that may place you away from the default sit position or to rotate you to face a particular direction. The offset and rotation can be altered using the program call llSitTarget(). Someone noticed that the offset can be hundreds of meters away. All teleports work sitting you at an offset that places you at your destination and then unsits you.

If you use the example scripts, most will send you flying around put you off the sim for a while or leave you 'falling' in one position. This happens as you are often placed inside an object and then made to unsit. The physics engine sees you are inside an object and applies a force to get you out of it. The force can be quite violent and enough to send you flying around a room or leave you floating in space.

I found that pauisng before the unsit command gives the normal sit processes to work so that the program has time to place you correctly.

The second problem was caused my moving the teleport. Since the sit position is relative, every time you moved the teleport you have to recompile the script as moving changes the offset. I noticed there is an event that tells you when an object has moved. I added a small script that recalculates your sit position each time you move the teleport.

// Simple teleport script
// by Asp Grelling
// 08MAY07


vector targetPos = <128.0, 250.0, 251.566>; //The target location

reset()
{
vector target;
target = (targetPos- llGetPos()) * (ZERO_ROTATION / llGetRot()); // Calculate offset
llSitTarget(target, ZERO_ROTATION); // Set the offset and rotation
llSetSitText(llGetObjectName());
}

default
{
state_entry()
{
reset();
}

on_rez(integer startup_param)
{
reset();
}

changed(integer change)
{
llSleep(0.2); // Sleep to give the program time to seat you
llUnSit(llAvatarOnSitTarget()); // Unsit the avatar
reset();
}

moving_end()
{
// Detect that the object has been moved
reset();
}
}

Moved from http://designasp.blogspot.com

No comments: