- Joined
- Sep 20, 2018
- Messages
- 4,049
// continuous rotate to face a moving target
// avatar must touch to have it rotate to face them (positive X face)
// avatar must touch to have it rotate to face them (positive X face)
Code:
key id_lookat = NULL_KEY;
integer bLookAt;
lookat()
{
// Avatar can move over time, so poll their pos & rot every half second ...
llSetTimerEvent(0.5);
}
pause()
{
// ... since all the work happens in the timer event. No timer, no results
llSetTimerEvent(0.0);
}
// clamp rotation to Z axis only
rotation clamp_to_Z(rotation r)
{
vector v = llRot2Euler(r)*RAD_TO_DEG;
v.x = 0;
v.y = 0;
return(llEuler2Rot(v*DEG_TO_RAD));
}
default
{
timer()
{
list l;
vector pos_lookat;
rotation rot_lookat;
vector dir;
rotation rot_work;
// if someone has touched, get their UUID and look at them ..."
if (id_lookat) {
// ... get the avatar's pos & rot ...
l = llGetObjectDetails(id_lookat, ([OBJECT_POS, OBJECT_ROT]));
pos_lookat = llList2Vector(l,0);
rot_lookat = llList2Rot(l,1);
// ... and calculate rotation for prim/object
dir = llVecNorm(pos_lookat - llGetPos());
rot_work = clamp_to_Z(llRotBetween(<1.0, 0.0, 0.0>, dir));
llSetRot(rot_work);
}
}
on_rez(integer param)
{
// force a reset on rez so the script can work
llResetScript();
}
touch_end(integer num_detected)
{
// flip bLookAt between on & off
bLookAt = !bLookAt;
if (bLookAt) {
id_lookat = llDetectedKey(0);
llWhisper(0, "Looking at "+llKey2Name(id_lookat));
lookat();
}
else {
llOwnerSay("stopped looking");
pause();
}
}
}
Last edited:

