Gun Script

From Second Life Wiki
Revision as of 10:12, 12 March 2011 by Rhonin Nissondorf (talk | contribs) (Created page with "Uses tons of useful functions and a great example for those starting out. I've had tons of fun with this and all its variants i've made in my time here in Second Life. Obviously…")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Uses tons of useful functions and a great example for those starting out. I've had tons of fun with this and all its variants i've made in my time here in Second Life. Obviously only can be used were you can rez and run scripts.



<lsl>

// Based on phillip linden's gunscript with many refinements by rhonin nissondorf.



integer on = TRUE; vector pos; rotation rot; vector offset;


// variables to be changed below. float BULLET_VELOCITY = 15.0; // change this to change the speed of the bullet. float REPEAT_DELAY = 0.30; //delay between bullets, i recommend you dont' set it to low. string gunsound = "gun"; // string; name of sound in inventory string ammo = "bullet 1.0"; //name of desired object to be shot out. string message1 = "stop playing with mine and ask for yours"; string message2 = " Hey put a message here"; //message when you touch it.

//dont' alter anything below if your not familiar with it.

default //The main state is the default state. When a script is compiled, reset or loaded, this is the state it enters by default. After the default state definition can follow additional state definitions which the script may use to change how and which events are handled. {

  state_entry()    // Triggered on any state transition and startup
    {
       llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH);
   

}

   run_time_permissions(integer perm)    //Triggered when an agent grants run time permissions to task ,its auto grant when attached.
   {
       if (perm)
       {
           llAttachToAvatar(ATTACH_RHAND);
           llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
           llStartAnimation("hold_R_bazooka");
       }
   }
   
  on_rez(integer st)  // Triggered when an object is rezzed

{


           llWhisper(0, "Attach me to your right hand, and enter mouselook to fire!");
            llPreloadSound(gunsound);

llResetScript();


       }
      touch_start(integer nd)   // Triggered by the start of agent clicking on object
       {
           if(on== TRUE)
     {
        llWhisper(0, message1);
       on= FALSE;
   }
   
   else if (on ==FALSE)
   {
       llWhisper(0, message2);
        on= TRUE;
       } 
      
          
       }
   
   
   attach(key id)    // When the object is dettached by an avatar 
   {
       if ( id == NULL_KEY)  
       {
         
       llStopAnimation("hold_R_bazooka");

}

   }
       
   changed(integer change)    //Various changes to the object/prim trigger this event.
   {
           if(change & CHANGED_OWNER)
       {
  
   llStopAnimation("hold_R_bazooka");
   llResetScript();

} }

   control(key owner, integer level, integer edge) // Result of llTakeControls library function call and user input.
   {
       if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)  //level is when you click ,edge would be if you let up.
       {
          
              
           
           //  Fire 1 bullet,,  the heart of the gun script.
           pos = llGetPos();
           rot = llGetRot();
           offset = <1.0, 0.0, 0.0>; 
           offset *= rot;
           pos += offset;
           llPointAt(pos); 
           vector fwd = llRot2Fwd(rot); 
           fwd *= BULLET_VELOCITY; 
           integer i = 5;
           rot *= llEuler2Rot(<0, PI_BY_TWO, 0>);
           llPlaySound(gunsound,1.0); // here "gunsound"is a variable defined above.
           llRezObject(ammo, pos, fwd, rot, 1); // here ammo is variable defined above.
           llSleep(REPEAT_DELAY); // ditto with  REPEAT_DELAY.
       }
      
       }
   
   

}