Gun Script

From Second Life Wiki
Revision as of 06:17, 13 June 2011 by Rhonin Nissondorf (talk | contribs) (this revision added perm track camera)
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>

//A combination of defunct freebie scripts were used to make this none copyrighted to my knowledge at least.



integer on = TRUE;



// 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. Must be in the inventory of the "gun". string message1 = "stop playing with mine and ask for yours"; //message when you touch it. string message2 = " Hey put a message here";

//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 start up
    {
       llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);
   

}

   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)
       {
         
       llRequestPermissions(llGetOwner(),PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS|PERMISSION_ATTACH|PERMISSION_TRACK_CAMERA);

}

   else if ( id == NULL_KEY)
       {
    llStopAnimation("hold_R_bazooka");
 llReleaseControls();
   }
    }   
    changed(integer change)
   {
           if(change & CHANGED_OWNER)
       {
  
   llResetScript();

} }

    control(key owner, integer level, integer edge)
   {
       if ((level & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
       {
           //  Mouse down
           if ((edge & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
           { 
              
           
         
           llPlaySound("gun",1.0);
          llRezAtRoot(ammo,llGetCameraPos()+<1.5,0,0>*llGetCameraRot(), BULLET_VELOCITY *llRot2Fwd(llGetCameraRot()),llGetCameraRot(),10); 
           llSleep(REPEAT_DELAY);
       }
      
       }
   
   }

}