Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Random Movement?

Cahliah Dassin
Registered User
Join date: 10 Sep 2005
Posts: 20
10-22-2005 13:41
I've been working on a script to move my 'pet' randomly, as long as it stays within 5 meters of me. It compiles fine, and gets to the beginning of the running state, but after that it doesn't do anything. I'm guessing it's a problem with my formula, but I figured I'd ask here.
So...
Any idea why this isn't working?

CODE

//This script will enable an object to wander freely, returning it to
//its home position if it strays more than 5m away.
vector home;
vector current;
vector target;
integer targetID;
integer distance;

default
{
state_entry()
{
llSetStatus (STATUS_PHYSICS, TRUE);
llSetStatus (STATUS_PHANTOM, TRUE);
llSetStatus (STATUS_ROTATE_X | STATUS_ROTATE_Y, FALSE);
llSetStatus (STATUS_BLOCK_GRAB, TRUE);
//Set current position as home
vector home = llGetPos();
vector current = llGetPos();
//Continue to next state
state running;
}
}
state running
{
state_entry()
{
llSay (0, "Running state activated");
//Set the vector 'target' to a random destination no more than 5m away
//from current.
vector target = (current + <llFrand(10) - 5,llFrand(10) - 5,0>);
targetID = llTarget (target, 1);
llMoveToTarget (target, 5);
}
at_target(integer tnum, vector targetpos, vector ourpos)
{
llTargetRemove (targetID);
llStopMoveToTarget();
vector current = llGetPos();
float distance = llVecDist (current, home);
if (distance > 5)
{
vector target = home;
}
if (distance < 5)
{
vector target = home;
}
else
{
state running;
}
}
}
Cahliah Dassin
Registered User
Join date: 10 Sep 2005
Posts: 20
10-22-2005 14:04
Fixed! Thank you, cua Curie! :)
RyeDin Meiji
Reluctant Entrepeneur
Join date: 15 Mar 2005
Posts: 124
10-24-2005 12:01
Might you post what the fix was? Not that I'm specifically dying to know, but it would be overall helpful for anyone wishing to learn something :D
_____________________
if (!you)
{
who();
}
Cahliah Dassin
Registered User
Join date: 10 Sep 2005
Posts: 20
10-24-2005 13:47
From: RyeDin Meiji
Might you post what the fix was? Not that I'm specifically dying to know, but it would be overall helpful for anyone wishing to learn something :D


No problem. Though I'm just going from memory because I don't have access to the actual script right now. :P

Basically, my problem was that I had the script seperated into three different states, all calling variables that were only defined in one state. I just condensed the whole thing into one state, repeated the llMoveToTarget, and voila!

CODE
//This script will enable an object to wander freely, returning it to
//its home position if it strays more than 5m away.
vector home;
vector current;
vector target;
integer targetID;
integer distance;

default
{
state_entry()
{
llSetStatus (STATUS_PHYSICS, TRUE);
llSetStatus (STATUS_PHANTOM, TRUE);
llSetStatus (STATUS_ROTATE_X | STATUS_ROTATE_Y, FALSE);
llSetStatus (STATUS_BLOCK_GRAB, TRUE);
//Set current position as home
vector home = llGetPos();
vector current = llGetPos();
//Set the vector 'target' to a random destination no more than 5m away
//from current.
vector target = (current + <llFrand(10) - 5,llFrand(10) - 5,0>);
targetID = llTarget (target, 1);
llMoveToTarget (target, 5);
}
at_target(integer tnum, vector targetpos, vector ourpos)
{
llTargetRemove (targetID);
llStopMoveToTarget();
vector current = llGetPos();
float distance = llVecDist (current, home);
if (distance > 5)
{
vector target = home;
}
if (distance < -5)
{
vector target = home;
}
else
{
vector target = (current + <llFrand(10) - 5,llFrand(10) - 5,0>);
}
targetID = llTarget (target, 1);
llMoveToTarget (target, 5);
}
}