Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Library: Basic Pet Script (Follow/Face Owner)

Logan Bauer
Inept Adept
Join date: 13 Jun 2004
Posts: 2,237
12-05-2004 11:57
This script will make an object follow it's owner and face towards it's owner, creating a very basic "pet". Make sure that the root prim of the object is facing a rotation of <0,0,0,1>, or <ZERO_ROTATION>, or just edit the object and put 0 in the X,Y, and Z rotations... OR, be prepaired for your pet to always face the wrong way! :eek:

CODE


////////////////////////////////////////////////////////////////
// Open Basic Follower/Facing Script, by Logan Bauer. //
////////////////////////////////////////////////////////////////
// OFFSET is the position of your pet in relation to it's owner's position.
// For example, in the default setting below, "vector offset =<-1,0,1>;"
// I.E. (x,y,z), the -1 puts it 1m back behind owner, the 0 means don't have
// it stay left or right, and 1 means have it stay 1m above it's owner.
// So, if you wanted the script to make it follow directly in front of you,
// and to the left, then you would change it to "vector offset =<1,1,0>;"


vector offset =<-1,0,1>;

startup()
{
vector pos = llGetPos();
llSetStatus(STATUS_ROTATE_Z,TRUE);
llSetStatus(STATUS_PHYSICS, TRUE);
key id = llGetOwner();
llSensorRemove();
llSensorRepeat("",llGetOwner(),AGENT,200,2*PI,.5);
}

default
{
state_entry()
{
startup();


}

on_rez(integer start_param)
{
startup();
}

sensor(integer total_number)
{
vector pos = llDetectedPos(0);
llMoveToTarget(pos+offset*llDetectedRot(0),.3);
llLookAt(pos, .1 , 1);
}
}
Millie Thompson
Resident Moderator
Join date: 18 Dec 2002
Posts: 364
12-19-2004 11:10
Shnazzy script, just one problem I have noticed, even with a root prim set to zero rotation (0, 0, 0) the object with the script rotates to point towards the avatar's pelvis. Thus causing unwanted rotations to ~ (209, 35, 141).

Thus my syringe follow drone was created with a permanent lock on your avatar's hiney. ^^
_____________________
Millie Thompson
I am a Resident Moderator. I am a volunteer moderator on this forum, NOT a Linden. If you have any issues or concerns with your Second Life experience please go to Second Life Support
Logan Bauer
Inept Adept
Join date: 13 Jun 2004
Posts: 2,237
12-26-2004 06:57
Hi Millie,

To get it to face/look at the Avatar's head instead of the Avatar's mid-section...
Quickest fix I can think of is change the last line from this:

llLookAt(pos, .1 , 1);

to this

llLookAt(pos+<0,0,1>, .1 , 1);

Er, I think... It's early in the morning here and my brain's still booting up. If that doesn't work, let me know.

And then if he was looking, say, too high and ABOVE your eye level, try adding +<0,0,0.8> to pos... OR if he was looking at your chest and wasn't looking high enough, try adding +<0,0,1.2>...
leptoon Angel
Registered User
Join date: 6 Apr 2005
Posts: 26
Nice!
04-09-2005 16:50
Very cool script. I use it all the time!
Remote Control
Registered User
Join date: 20 Aug 2005
Posts: 1
08-24-2005 03:04
Fun script! Thank you for sharing it.
Alexander Yeats
Registered User
Join date: 8 Sep 2005
Posts: 188
09-16-2005 09:57
Any thoughts on how to have the pet face the exact direction the owner is facing?
Unit4 Grasshopper
Registered User
Join date: 13 Mar 2005
Posts: 2
09-18-2005 17:07
try this instead for the sensor:

sensor(integer total_number)
{
vector pos = llDetectedPos(0);
rotation rot = llDetectedRot(0);
llMoveToTarget(pos+offset*llDetectedRot(0),.3);
llRotLookAt(rot, .1 , 1);
}

that should make it look the same way as you.
Unit4 Grasshopper
Registered User
Join date: 13 Mar 2005
Posts: 2
09-18-2005 17:25
try this for the sensor:

sensor(integer total_number)
{
vector pos = llDetectedPos(0);
rotation rot = llDetectedRot(0);
llMoveToTarget(pos+offset*llDetectedRot(0),.3);
llLookAt(rot, .1 , 1);
}

that should make it look in the same direction as you.
Morphy Mousehold
Registered User
Join date: 11 May 2005
Posts: 5
09-23-2005 17:57
I have a question to add. What would be involved in making two, three, or more follow the owner in a chain-like prosession? Or would this level of many-scripts, since each prim would have this "follow the leader" script, be too hard on the sim?
Logan Bauer
Inept Adept
Join date: 13 Jun 2004
Posts: 2,237
09-24-2005 12:08
From: Morphy Mousehold
I have a question to add. What would be involved in making two, three, or more follow the owner in a chain-like prosession? Or would this level of many-scripts, since each prim would have this "follow the leader" script, be too hard on the sim?


1. Modifying the offset at which the object follows you, I think that's about it... 2.And Yes, possibly, depending on how many following objects you have and what they're doing (I.E are they just following you or running other scripts and doing other things)

With the small line of pets I made, I tested rezzing a bunch of them in a sandbox with different follow-offsets (also known as the "moogle parade" experiments), and while about up to a dozen didn't visibly impact the server, when I tried to have around 50 it wasn't a good thing - lag increased, fps decreased, ect...
Logan Bauer
Inept Adept
Join date: 13 Jun 2004
Posts: 2,237
Disclaimer/Cautionary Notice
09-25-2005 09:33
One other important thing to keep in mind. The line that reads :

llSensorRepeat("",llGetOwner(),AGENT,200,2*PI,.5);

at the end, the ".5", means that this sensor is called every .5 seconds. Now, this is a pretty frequent rate, you could probably even turn it to about 2 or 3 seconds for most projects - But for a pet that follows you fairly smoothly, .5 seemed to work best for me.

IF you're going to have multiple things follow you, I'd make it at least 1-2 seconds.

Sensors with high repeat rates, and things with physics enabled, both tend to contribute to lag, so be careful!
Mustelid Carnot
Made from quality ferrets
Join date: 7 Sep 2005
Posts: 106
09-26-2005 10:24
Another pet-following question:

I'd like to be able to have my pet bob up and down slightly (z axis I think?) as it follows. Is this impossible? The only examples of this sort of script I've for a vehicle are broken now :/
Logan Bauer
Inept Adept
Join date: 13 Jun 2004
Posts: 2,237
Hmmm
10-04-2005 18:26
Well, I'm sure there is a much better way to do this, using applyimpulse or sin and cosine or... something... BUT, this should do a bobbing motion until someone who knows more about math and real scripting than me posts a better solution. :D

CODE

// I'm sure there's a more elegant way to do this, hopefully someone who can will streamline or redo this better... "floaty" is our bob-modifier, fliptog is an integer to mark if we're bobbing up or down.

///////// Play around with these variables to adjust bob distance and speed
float floatdist = 0.1; // This is the distance we bob up and down.
float floatmod = 0.02; // This is the step or increment we move each time, basically how fast we bob up and down.(the bigger the number, the faster we move.)
///////////////////////////////////////////////////////////////////////





float floaty;
integer flippertog;

float floatbase;


vector offset =<-1,0,1>;

startup()
{
floatbase = offset.z;
flippertog = TRUE;
vector pos = llGetPos();
llSetStatus(STATUS_ROTATE_Z,TRUE);
llSetStatus(STATUS_PHYSICS, TRUE);
key id = llGetOwner();
llSensorRemove();
llSensorRepeat("",llGetOwner(),AGENT,200,2*PI,.5);
}

floatcountplus()
{
offset = <offset.x,offset.y,floatbase>; //Return to base z offset.
if (flippertog == TRUE)//Going up?
{
floaty = floaty + floatmod;
if (floaty > floatdist) // We just added, are we capped?
{
flippertog = FALSE;

}
}

else //flippertog must be false..., we must be moving down.
{
floaty = floaty - floatmod;
if (floaty < -floatdist) // We just added, are we capped?
{
flippertog = TRUE;
}
}
// llOwnerSay((string)floaty + " > " + (string)flippertog);
floatbase = offset.z;
offset = <offset.x,offset.y,offset.z+floaty>;
}

default
{
state_entry()
{
startup();


}

on_rez(integer start_param)
{
startup();
}

sensor(integer total_number)
{
floatcountplus();
vector pos = llDetectedPos(0);
llMoveToTarget(pos+offset*llDetectedRot(0),.3);
llLookAt(pos, .1 , 1);
}
}


Erebus Vasilopita
Registered User
Join date: 14 Oct 2005
Posts: 1
10-21-2005 21:50
will this on work for birds and and incets? i'm still having problems with the visual acspect of this as well
Logan Bauer
Inept Adept
Join date: 13 Jun 2004
Posts: 2,237
10-25-2005 10:09
From: Erebus Vasilopita
will this on work for birds and and incets? i'm still having problems with the visual acspect of this as well


It should work for about anything you drop it into - assuming you want the birds and insects to follow you. What visual aspect are you having difficulties?
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
Follow chain
10-26-2005 14:43
If you're going to do a follow chain properly, you don't have them follow the av, you have them follow each other.

Have the first one rez the second one with a parameter of (1), then in the on_rez, get it to set its name to "follower 1" and rez "follower 2". Now follower(n) can follow follower(n-1) by with

CODE

on_rez(integer param)
{
llSetObjectName("Follower "+(string)param)
if(param = 0) follow_agent();
else follow_object("Follower "+(string)(param-1));
if(param >= 10) return
llRezObject("Follower", <0,0,2>, ZERO_VECTOR, ZERO_ROTATION, param+1);
}

object_rez(key id)
{
llGiveInventory(id, "Follower");
}
Feynt Mistral
Registered User
Join date: 24 Sep 2005
Posts: 551
11-25-2005 23:45
I was doing some reading through the forum archives and came across someone who wanted to do tail movement. I couldn't help but think of the movement chaining here, could this idea produce a tail that moves without the aid of motion files? Also, would there be a possible way to restrict the amount a certain segment could turn in relation to its peers? Like, no more than 15 degrees off so a 5 segment tail couldn't go sweeping through your torso or between your legs when you stop?
Feynt Mistral
Registered User
Join date: 24 Sep 2005
Posts: 551
11-27-2005 14:00
I was wondering if it would be possible to use the original model's euler rotation to correct the model in case it's not 0,0,0 rotated. Wouldn't there be a way to first rotate the object to the correct orientation, and then tilt that to the correct direction the player's facing?
BronYAurStomp Lightworker
Beep Boop Boop Bop
Join date: 11 Dec 2005
Posts: 25
12-11-2005 12:08
what's the deal with using scripts on linked objects?
Tsuno Soyinka
Registered User
Join date: 28 Oct 2005
Posts: 15
12-13-2005 06:50
I am planning to make a tail that will use the lag betwen follow scripts to create a diminishing change in position.
is there a way to get the key of the newly rezzed object?
Sydney Alexander
Registered User
Join date: 19 Feb 2005
Posts: 69
12-16-2005 09:20
This is a fun little script, but I have some questions...

I see in this thread how to get the "pet" to look at your head or mid section and so on. I also see a little tweak that will have your pet look the same direction as you not at you. When I use this change my prim is reset to 000 rotation. I have a cube with a cone as the nose... I want the cone facing forward and looking in the same direction as my avatar. What am I missing? Is there some place in the script to set the default rotation of the prim?

Thanks

From: Logan Bauer
Well, I'm sure there is a much better way to do this, using applyimpulse or sin and cosine or... something... BUT, this should do a bobbing motion until someone who knows more about math and real scripting than me posts a better solution. :D

CODE

// I'm sure there's a more elegant way to do this, hopefully someone who can will streamline or redo this better... "floaty" is our bob-modifier, fliptog is an integer to mark if we're bobbing up or down.

///////// Play around with these variables to adjust bob distance and speed
float floatdist = 0.1; // This is the distance we bob up and down.
float floatmod = 0.02; // This is the step or increment we move each time, basically how fast we bob up and down.(the bigger the number, the faster we move.)
///////////////////////////////////////////////////////////////////////





float floaty;
integer flippertog;

float floatbase;


vector offset =<-1,0,1>;

startup()
{
floatbase = offset.z;
flippertog = TRUE;
vector pos = llGetPos();
llSetStatus(STATUS_ROTATE_Z,TRUE);
llSetStatus(STATUS_PHYSICS, TRUE);
key id = llGetOwner();
llSensorRemove();
llSensorRepeat("",llGetOwner(),AGENT,200,2*PI,.5);
}

floatcountplus()
{
offset = <offset.x,offset.y,floatbase>; //Return to base z offset.
if (flippertog == TRUE)//Going up?
{
floaty = floaty + floatmod;
if (floaty > floatdist) // We just added, are we capped?
{
flippertog = FALSE;

}
}

else //flippertog must be false..., we must be moving down.
{
floaty = floaty - floatmod;
if (floaty < -floatdist) // We just added, are we capped?
{
flippertog = TRUE;
}
}
// llOwnerSay((string)floaty + " > " + (string)flippertog);
floatbase = offset.z;
offset = <offset.x,offset.y,offset.z+floaty>;
}

default
{
state_entry()
{
startup();


}

on_rez(integer start_param)
{
startup();
}

sensor(integer total_number)
{
floatcountplus();
vector pos = llDetectedPos(0);
llMoveToTarget(pos+offset*llDetectedRot(0),.3);
llLookAt(pos, .1 , 1);
}
}


Sydney Alexander
Registered User
Join date: 19 Feb 2005
Posts: 69
12-16-2005 09:34
Fun Script. When I use it and add the tweak listed in this thread to make the pet look in the same direction at the avatart not at it. This change resets the prims default rotation. I have a cube with a cone as the nose... just for testing, and it poitns the cone straight up. Is there someplace to set the default rotations of the pet?

thanks,


From: Logan Bauer
Well, I'm sure there is a much better way to do this, using applyimpulse or sin and cosine or... something... BUT, this should do a bobbing motion until someone who knows more about math and real scripting than me posts a better solution. :D

CODE

// I'm sure there's a more elegant way to do this, hopefully someone who can will streamline or redo this better... "floaty" is our bob-modifier, fliptog is an integer to mark if we're bobbing up or down.

///////// Play around with these variables to adjust bob distance and speed
float floatdist = 0.1; // This is the distance we bob up and down.
float floatmod = 0.02; // This is the step or increment we move each time, basically how fast we bob up and down.(the bigger the number, the faster we move.)
///////////////////////////////////////////////////////////////////////





float floaty;
integer flippertog;

float floatbase;


vector offset =<-1,0,1>;

startup()
{
floatbase = offset.z;
flippertog = TRUE;
vector pos = llGetPos();
llSetStatus(STATUS_ROTATE_Z,TRUE);
llSetStatus(STATUS_PHYSICS, TRUE);
key id = llGetOwner();
llSensorRemove();
llSensorRepeat("",llGetOwner(),AGENT,200,2*PI,.5);
}

floatcountplus()
{
offset = <offset.x,offset.y,floatbase>; //Return to base z offset.
if (flippertog == TRUE)//Going up?
{
floaty = floaty + floatmod;
if (floaty > floatdist) // We just added, are we capped?
{
flippertog = FALSE;

}
}

else //flippertog must be false..., we must be moving down.
{
floaty = floaty - floatmod;
if (floaty < -floatdist) // We just added, are we capped?
{
flippertog = TRUE;
}
}
// llOwnerSay((string)floaty + " > " + (string)flippertog);
floatbase = offset.z;
offset = <offset.x,offset.y,offset.z+floaty>;
}

default
{
state_entry()
{
startup();


}

on_rez(integer start_param)
{
startup();
}

sensor(integer total_number)
{
floatcountplus();
vector pos = llDetectedPos(0);
llMoveToTarget(pos+offset*llDetectedRot(0),.3);
llLookAt(pos, .1 , 1);
}
}


Sydney Alexander
Registered User
Join date: 19 Feb 2005
Posts: 69
12-16-2005 09:53
This is a fun little script, but having some trouble. I have a cube with a cone for the nose. the script works fine until I try the suggested tweak to have the pet look in the same direction as your avatar not at them... Well when I do this the pet is reste t ooo rotation and my cone is pointing straight up. I know I ma missing something in the klinking of the prims or the script that sets a default for a prim. Help!!!

thanks

CODE

try this instead for the sensor:

sensor(integer total_number)
{
vector pos = llDetectedPos(0);
rotation rot = llDetectedRot(0);
llMoveToTarget(pos+offset*llDetectedRot(0),.3);
llRotLookAt(rot, .1 , 1);
}

that should make it look the same way as you.
Zeboc Bigwig
Registered User
Join date: 16 Dec 2005
Posts: 1
01-08-2006 20:05
Im having the same exact problem.

No matter what the originating rotation of the prim is, when I activate it, it ALWAYS immediately turns the wrong way and goes upside down.

I have tried everything I can think of. Im very new at using scripts and building, but I had some experts helping and they cant get it right either.

:( Help!
Jade Bard
Registered User
Join date: 7 Jul 2004
Posts: 106
01-10-2006 17:46
How would i have it, so that the pet would look at you in front of you.
Like I Rez it, and it flies into the front of me [facing me.] ?
1 2