Jump to content

Need help with Script that returns object to original position


Loki Eliot
 Share

You are about to reply to a thread that has been inactive for 4097 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Im making tin cans that you can shoot with my slingshot. I want then to return to where they were last placed before being shot. To do this i made it so the can when shot  GetsPosition before jumping in the air. Then after 15 seconds the can returns to position.

So far so good, BUT it was discoverd in testing that when lots of people are shooting at the cans, sometimes a can will get hit while its up in the air or roling on the cround and this tells the can to GetPosition again meaning the can wont return to its position before it was shot the first time. 

Is there a way to stop the can registering being hit for 15 seconds after its first hit? I am not a very good scripter, i mainly cut and paste so sorry if my script is unprofesional :-p

here is my script 

default
{
state_entry()
    {
         llSetStatus(STATUS_PHYSICS, FALSE);
         llSetAlpha(100,ALL_SIDES);  
          llSetAlpha(0,1);  
        }
        
collision(integer total_number)
    {
       if (llDetectedName(0) == "shot")
        {
            vector pos = llGetPos();
             rotation rot = llGetRot();
          llSetStatus(STATUS_PHYSICS, TRUE);
         llApplyImpulse(llGetMass()*<0,0,10>,TRUE);
         llSleep(0.3);

         llSetAlpha(100,ALL_SIDES);  
          llSetAlpha(0,0); 
          llTriggerSound("tincans", 10.0);
          llSleep(15);

          llSetStatus(STATUS_PHYSICS, FALSE);
          llSetPos(pos);
          llSetRot(rot);
          llSleep(0.6);
          llSetAlpha(100,ALL_SIDES);  
          llSetAlpha(0,1); 
          llSleep(0.6);

          llResetScript();
                 }
                }
            }

 Any help would be most apreciated

Link to comment
Share on other sites

The simplest thing to do is grab the position and rotation when the can gets hit, an start a timer that's set for 15 seconds.  Get rid of all of the llSleep commands that just mess up things.  Then, when the timer event fires, reset the can's position and rotation to the originals.

ETA:  And while you're at it, remove all the llSetAlpha commands too.  There's no point in issuing one that sets the can to 100% alpha (that's 1.00, BTW) and then immediately canceling that out by setting it to 0.0. 

Link to comment
Share on other sites

Use an integer as a boolean for true/false, so you can toggle between hit/not hit.  Add a check for the integer in your collision to ensure the can hasn't been hit, then react accordingly; if hit nothing happens.  Set the timer for 15 seconds and then execute the code to place the can back where it needs to go.  No need to reset the script if you set the integer to false (not hit).  Since the pos and rot variables are referenced from both the timer and collision events, they have to be made global for the script.  All added lines have // comments.

 

// globals, for use throughout the script
integer already_hit; // boolean toggle (true/false)vector pos; // position
rotation rot; // rotation

default{ state_entry() { llSetStatus(STATUS_PHYSICS, FALSE); llSetAlpha(100,ALL_SIDES); llSetAlpha(0,1); } collision(integer total_number) { if (llDetectedName(0) == "shot" && already_hit == FALSE) // if not already hit { already_hit = TRUE; // set as hit pos = llGetPos(); rot = llGetRot(); llSetStatus(STATUS_PHYSICS, TRUE); llApplyImpulse(llGetMass()*<0,0,10>,TRUE); llSleep(0.3); llSetAlpha(100,ALL_SIDES); llSetAlpha(0,0); llTriggerSound("tincans", 10.0); llSetTimerEvent(15); // start timer (count to 15) } } timer() // timer event (triggers after count to 15) { llSetTimerEvent(0); // stop timer, no further need for it
// can reset lines, moved from collision event llSetStatus(STATUS_PHYSICS, FALSE); llSetRegionPos(pos); // just in case object is over 10m from original pos (llSetPos limited to 10m) llSetRot(rot); llSleep(0.6); llSetAlpha(100,ALL_SIDES); llSetAlpha(0,1); llSleep(0.6); already_hit = FALSE; // reset as not hit } }

 

  • Like 1
Link to comment
Share on other sites

Thank you so much for cleaning up my script. Unfortunitly what i originally thought was the reason for Cans not returning to their original place is incorrect. 

Even with this tidied up script the cans after a while stop returning to original place, so im not sure why that is. :-s a mystery to me. Could it be something to do with cans rolling more than 10 metres away?

Link to comment
Share on other sites

OK, so I got silly.  Try this ...

vector gHome;integer gHit;default{    collision_start(integer numr)    {        if (llDetectedName(0) == "shot")        {            if (!gHit)            {                llSetTimerEvent(15.0);                gHome = llGetPos();                gHit = TRUE;            }            llSetStatus(STATUS_PHYSICS, TRUE);// llTriggerSound("tincans",1.0);            llApplyImpulse(llGetMass()*<0,0,5.0>,TRUE);            llApplyRotationalImpulse(llGetMass()*<llFrand(1.0),llFrand(1.0),llFrand(1.0)>,TRUE);            llResetTime();        }    }        land_collision(vector where)    {        if (llGetTime() < 0.5)        {            llResetTime();            llApplyImpulse(llGetMass()*<0,0,llFrand(1.0)>,TRUE);            llApplyRotationalImpulse(llGetMass()*<llFrand(1.0),llFrand(1.0),llFrand(1.0)>,TRUE);        }    }        timer()    {        llSetStatus(STATUS_PHYSICS,FALSE);        gHit = FALSE;        llSetRegionPos(gHome);  // Send the can home, even if more than 10m away        llSetRot(ZERO_ROTATION); // Sets the can upright        llSetTimerEvent(0.0);    }}

 

  • Like 1
Link to comment
Share on other sites

Hi Rolig,

thought I'd try your script with a 2 prim object, this does return to the upright after being hit BUT not its original position...

how would i go about achieving this?

And... how do i reduce the force on the objects from the impact? (So they dont fly off quite so much.)

Thanks!

Emma :)

 

P.S. And why does my banner look all wrong now im using Google Chrome rather than Opera?? Meh...

Link to comment
Share on other sites

Gee, Emma. .... I can't think of any reason why it shouldn't return to its home position, assuming that you copied the script as written.  It makes no difference whether an object is made of one prim or several.  Is the can bouncing into someone else's parcel --- one with scripts turned off?

As for changing the force.... that's what the llApplyImpulse amd llApplyRotationalImpulse functions do.  All I did was play around with numbers that looked right until I got the beer can to look realistic when it gets hit.  If it seems like 5.0 is too much, use a smaller number.  You may want to mess with the similar numbers in the land_collision event too.  Those apply a random bump or spin if the beer can hits the ground within a half second of when you shoot it.  Again, I just fiddled wih the maximum size of the impulses until they looked right. 

Link to comment
Share on other sites

Hi Rolig :)

Thanks for the speedy reply...

I copied your script again and remade the prims and everything is working fine now.

Having fun experimenting with this now - if only I wasn't such an awful shot!

(Considers whether it's possible to make bullets which look the same size as the competitors' but are partly transparent and massive in size.... :D)

 

Emma :)

 

Link to comment
Share on other sites


Loki Eliot wrote:

Thank you so much for cleaning up my script. Unfortunitly what i originally thought was the reason for Cans not returning to their original place is incorrect. 

Even with this tidied up script the cans after a while stop returning to original place, so im not sure why that is. :-s a mystery to me. Could it be something to do with cans rolling more than 10 metres away?

Yes, that is most likely the case.  llSetPos can only move an object 10m from its current location.  Use llSetRegionPos instead.

 

 

Link to comment
Share on other sites

You're probably right, if you get a fast-moving beer can.  When I started playing with it this morning, I was trying to make a can that took a nice initial bounce but then sort of dribbled along the ground for a while after it landed.  This one just looked better with those random motions added in a land_collision event than if I trusted physics to tumble the can by itself.  I take the experimental approach with these things, and I was in a fiddly mood.  :smileytongue: 

Link to comment
Share on other sites

  • 3 weeks later...

Loki was nice enough to send me a copy of the sling shot project.  It's loads of fun shooting those cans.  Great product Loki.  Just need a little tweaking to keep those script error messages ("Invalid force in llApplyRotationalImpulse.") from popping up.  Also, if you play near a parcel border you can potentially lose a can here and there, but no biggie.

Link to comment
Share on other sites

  • 6 months later...

I want to be able to roll a ball and have it come back to its original position after a set amount of time.

 

I tried using parts of the script given in this discussion, and it ALMOST works, but after the ball returns to the original position, the "Physical" box is no longer checked, so I cannot roll the ball again.

 

Anybody have any ideas?

Here is the script I am using:

 

vector gHome;

integer gHit;

default

{

touch_start(integer numr) 

{             

llSetTimerEvent(5.0);             

gHome = llGetPos();            

llSetRegionPos(gHome);                 

llResetTime();        

}        

timer()    

{        

llSetStatus(STATUS_PHYSICS,FALSE);        

gHit = FALSE;        

llSetRegionPos(gHome);        

llSetTimerEvent(0.0);           

}

}

 

 

Link to comment
Share on other sites

  • 2 weeks later...
You are about to reply to a thread that has been inactive for 4097 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...