Advertisement
Guest User

Basic Motorcycle script

a guest
Apr 17th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
LScript 12.37 KB | None | 0 0
  1. // :CATEGORY:Positioning
  2. // :NAME:Car
  3. // :AUTHOR:Anonymous
  4. // :CREATED:2010-01-10 05:20:56.000
  5. // :EDITED:2013-09-18 15:38:50
  6. // :ID:151
  7. // :NUM:219
  8. // :REV:1.0
  9. // :WORLD:Second Life
  10. // :DESCRIPTION:
  11. // Car.lsl
  12. // :CODE:
  13.  
  14. //Basic Motorcycle Script
  15. //
  16. // by Cory
  17. // commented by Ben
  18.  
  19. //The new vehicle action allows us to make any physical object in Second Life a vehicle. This script is a good example of a
  20. // very basic vehicle that is done very well.  
  21.  
  22. integer loopsnd = 0;
  23.  
  24. default
  25. {  
  26.  
  27.     //There are several things that we need to do to define vehicle, and how the user interacts with it.  It makes sense to
  28.     // do this right away, in state_entry.
  29.     state_entry()
  30.     {
  31.        
  32.         //We can change the text in the pie menu to more accurately reflecet the situation.  The default text is "Sit" but in
  33.         // some instances we want you to know you can drive or ride a vehicle by sitting on it. The llSetSitText function will
  34.         // do this.
  35.         llSetSitText("Drive");
  36.        
  37.         //Since you want this to be ridden, we need to make sure that the avatar "rides" it in a acceptable position
  38.         // and the camera allows the driver to see what is going on.
  39.         //
  40.         //llSitTarget is a new function that lets us define how an avatar will orient itself when sitting.
  41.         // The vector is the offset that your avatar's center will be from the parent object's center.  The
  42.         // rotation is bassed off the positive x axis of the parent. For this motorcycle, we need you to sit in a way
  43.         // that looks right with the motorcycle sit animation, so we have your avatar sit slightly offset from the seat.
  44.         llSitTarget(<0.3, 0.0, 0.55>, ZERO_ROTATION);
  45.        
  46.         //To set the camera, we need to set where the camera is, and what it is looking at.  By default, it will
  47.         // be looking at your avatar's torso, from a position above and behind. It will also be free to rotate around your
  48.         // avatar when "turning."
  49.         //
  50.         //For the motorcycle, we are going to set the camera to be behind the cycle, looking at a point in front of it.
  51.         // Due to the orientation of the parent object, this will appear to be looking down on the avatar as they navigate
  52.         // course.
  53.         llSetCameraEyeOffset(<-5.0, -0.00, 2.0>);
  54.         llSetCameraAtOffset(<3.0, 0.0, 2.0>);
  55.        
  56.         //Ask cory "why?"
  57.         llSetVehicleFlags(-1);
  58.        
  59.         //To make an object a vehicle, we need to define it as a vehicle.  This is done by assigning it a vehicle type.
  60.         // A vehicle type is a predefined set of paramaeters that describe how the physics engine should let your
  61.         // object move. If the type is set to VEHICLE_TYPE_NONE it will no longer be a vehicle.  
  62.         //
  63.         //The motorcycle uses the car type on the assumption that this will be the closest to how a motorcycle should work.
  64.         // Any type could be used, and all the parameters redefined later.
  65.         llSetVehicleType(VEHICLE_TYPE_CAR);
  66.        
  67.        
  68.         //While the type defines all the parameters, a motorcycle is not a car, so we need to change some parameters
  69.         // to make it behave correctly.
  70.        
  71.         //The vehicle flags let us set specific behaviors for a vehicle that would not be covered by the more general
  72.         // parameters. <more needed>
  73.         llSetVehicleFlags(VEHICLE_FLAG_NO_FLY_UP | VEHICLE_FLAG_LIMIT_ROLL_ONLY);
  74.        
  75.         //To redefine parameters, we use the function llSetVehicleHippoParam where Hippo is the variable type of the
  76.         // parameter (float, vector, or rotation).
  77.         //
  78.         //Most parameters come in pairs, and efficiency and a timescale. The efficiency defines <more>, while the timescale
  79.         // defines the time it takes to achive that effect.  
  80.         //
  81.         //In a virtual world, a motorcycle is a motorcycle because it looks and moves like a motorcycle.  The look is
  82.         // up to the artist who creates the model.  We get to define how it moves.  The most basic properties of movement
  83.         // can be thought of as the angular deflection (points in the way it moves) and the linear deflection (moves in the
  84.         // way it points).  A dart would have a high angular deflection, and a low linear deflection.  A motorcycle has
  85.         // a low linear deflection and a high linear deflection, it goes where the wheels send it. The timescales for these
  86.         // behaviors are kept pretty short.
  87.         llSetVehicleFloatParam(VEHICLE_ANGULAR_DEFLECTION_EFFICIENCY, 0.2);
  88.         llSetVehicleFloatParam(VEHICLE_LINEAR_DEFLECTION_EFFICIENCY, 0.80);
  89.         llSetVehicleFloatParam(VEHICLE_ANGULAR_DEFLECTION_TIMESCALE, 0.10);
  90.         llSetVehicleFloatParam(VEHICLE_LINEAR_DEFLECTION_TIMESCALE, 0.10);
  91.        
  92.         //A bobsled could get by without anything making it go or turn except for a icey hill. A motorcycle, however, has
  93.         // a motor and can be steered.  In LSL, these are linear and angular motors.  The linear motor is a push, the angular
  94.         // motor is a twist.  We apply these motors when we use the controls, but there is some set up to do.  The motor
  95.         // timescale controls how long it takes to get the full effect  of the motor, basically acceleration. The motor decay
  96.         // timescale defines how long the motor stays at that strength - how slowly you let off the gas pedal.
  97.         llSetVehicleFloatParam(VEHICLE_LINEAR_MOTOR_TIMESCALE, 0.3);
  98.         llSetVehicleFloatParam(VEHICLE_LINEAR_MOTOR_DECAY_TIMESCALE, 0.2);
  99.         llSetVehicleFloatParam(VEHICLE_ANGULAR_MOTOR_TIMESCALE, 0.3);
  100.         llSetVehicleFloatParam(VEHICLE_ANGULAR_MOTOR_DECAY_TIMESCALE, 0.1);
  101.        
  102.         //<wait for andrew's changes>
  103.         llSetVehicleVectorParam(VEHICLE_LINEAR_FRICTION_TIMESCALE, <1000.0, 2.0, 1000.0>);
  104.         llSetVehicleVectorParam(VEHICLE_ANGULAR_FRICTION_TIMESCALE, <10.0, 10.0, 1000.0>);
  105.        
  106.         //We are using a couple of tricks to make the motorcycle look like a real motorcycle.  We use an animated texture to
  107.         // spin the wheels.  The actual object can not rely on the real world physics that lets a motorcycle stay upright.
  108.         // We use the vertical attractor parameter to make the object try to stay upright. The vertical attractor also allows
  109.         // us to make the vehicle bank, or lean into turns.
  110.         //
  111.         //<NOT SURE IF FOLLOWING IS RIGHT>
  112.         //The vertical attaction efficiency is slightly missnamed, as it should be "coefficient." Basically, it controls
  113.         // if we critically damp to vertical, or "wobble" more.  The timescale will control how fast we go back to vertical, and
  114.         // thus how strong the vertical attractor is.
  115.         //
  116.         //We want people to be able to lean into turns,<ugh>
  117.         llSetVehicleFloatParam(VEHICLE_VERTICAL_ATTRACTION_EFFICIENCY, 0.90);
  118.         llSetVehicleFloatParam(VEHICLE_VERTICAL_ATTRACTION_TIMESCALE, 0.90);
  119.        
  120.         //Banking means that if we rotate on the roll axis, we will also rotate on the yaw axis, meaning that our motorcycle will lean to the
  121.         // side as we turn. Not only is this one of the things that make it look like a real motorcycle, it makes it look really cool too. The
  122.         // higher the banking efficiency, the more "turn" for your "lean".  This motorcycle is made to be pretty responsive, so we have a high
  123.         // efficiency and a very low timescale.
  124.         llSetVehicleFloatParam(VEHICLE_BANKING_EFFICIENCY, 0.05);
  125.         llSetVehicleFloatParam(VEHICLE_BANKING_TIMESCALE, 0.1);
  126.        
  127.         llCollisionSound("", 0.0);
  128.     }
  129.    
  130.    
  131.     //A sitting avatar is treated like a extra linked primitive, which means that we can capture when someone sits on the
  132.     // vehicle by looking for the changed event, specifically, a link change.
  133.     changed(integer change)
  134.     {
  135.         //Make sure that the change is a link, so most likely to be a sitting avatar.
  136.         if (change & CHANGED_LINK)
  137.         {
  138.             //The llAvatarSitOnTarget function will let us find the key of an avatar that sits on an object using llSitTarget
  139.             // which we defined in the state_entry event. We can use this to make sure that only the owner can drive our vehicle.
  140.             // We can also use this to find if the avatar is sitting, or is getting up, because both will be a link change.
  141.             // If the avatar is sitting down, it will return its key, otherwise it will return a null key when it stands up.
  142.             key agent = llAvatarOnSitTarget();
  143.            
  144.             //If sitting down.
  145.             if (agent)
  146.             {
  147.                 //We don't want random punks to come stealing our motorcycle! The simple solution is to unsit them,
  148.                 // and for kicks, send um flying.
  149.                 if (agent != llGetOwner())
  150.                 {
  151.                     llSay(0, "You aren't the owner");
  152.                     llUnSit(agent);
  153.                     llPushObject(agent, <0,0,100>, ZERO_VECTOR, FALSE);
  154.                 }
  155.                 // If you are the owner, lets ride!
  156.                 else
  157.                 {
  158.                     //The vehicle works with the physics engine, so in order for a object to act like a vehicle, it must first be
  159.                     // set physical.
  160.                     llSetStatus(STATUS_PHYSICS, TRUE);
  161.                     //There is an assumption that if you are going to choose to sit on a vehicle, you are implicitly giving
  162.                     // permission to let that vehicle act on your controls, and to set your permissions, so the end user
  163.                     // is no longer asked for permission.  However, you still need to request these permissions, so all the
  164.                     // paperwork is filed.
  165.                     llRequestPermissions(agent, PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS);
  166.                 }
  167.             }
  168.             //The null key has been returned, so no one is driving anymore.
  169.             else
  170.             {
  171.                 //Clean up everything.  Set things nonphysical so they don't slow down the simulator.  Release controls so the
  172.                 // avatar move, and stop forcing the animations.
  173.                 llSetStatus(STATUS_PHYSICS, FALSE);
  174.                 llReleaseControls();
  175.                 llStopAnimation("sit");
  176.             }
  177.         }
  178.        
  179.     }
  180.    
  181.     //Because we still need to request permissions, the run_time_permissions event still occurs, and is the perfect place to start
  182.     // the sitting animation and take controls.
  183.     run_time_permissions(integer perm)
  184.     {
  185.         if (perm)
  186.         {
  187.             llStartAnimation("sit");
  188.             llTakeControls(CONTROL_FWD | CONTROL_BACK | CONTROL_RIGHT | CONTROL_LEFT | CONTROL_ROT_RIGHT | CONTROL_ROT_LEFT, TRUE, FALSE);
  189.         }
  190.     }
  191.    
  192.     //If we want to drive this motorcycle, we need to use the controls.  
  193.     control(key id, integer level, integer edge)
  194.     {
  195.         //We will apply motors according to what control is used.  For forward and back, a linear motor is applied with a vector
  196.         // parameter.
  197.         vector angular_motor;
  198.  
  199.         if (level & edge & CONTROL_FWD)
  200.         {
  201.             // Forward key was initially pressed
  202.             llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, <50,0,0>);
  203.            
  204.         }
  205.         else if ((edge & CONTROL_FWD) && ((level & CONTROL_FWD) == FALSE))
  206.         {
  207.             // Forward key was just released
  208.             llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, <0,0,0>);
  209.            
  210.             loopsnd = 0;
  211.         }
  212.         else if (level & CONTROL_FWD)
  213.         {
  214.             // Forward key was just released
  215.             llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, <50,0,0>);
  216.            
  217.            
  218.         }
  219.        
  220.         if(level & CONTROL_BACK)
  221.         {
  222.             llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, <-10,0,0>);
  223.         }
  224.         if(level & (CONTROL_RIGHT|CONTROL_ROT_RIGHT))
  225.         {
  226.             angular_motor.x += 100;
  227.             angular_motor.z -= 100;
  228.         }
  229.         if(level & (CONTROL_LEFT|CONTROL_ROT_LEFT))
  230.         {
  231.             angular_motor.x -= 100;
  232.             angular_motor.z += 100;
  233.         }
  234.         if(level & (CONTROL_UP))
  235.         {
  236.             angular_motor.y -= 50;
  237.         }
  238.    
  239.         llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, angular_motor);
  240.     }
  241.    
  242. }
  243. // END //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement