Introduction
This script may be used for any large gun, but is designed particularly for a moveable gun such as that found in a tank or similar vehicle.
Construction
The following elements are necessary or recommended for the vehicle gun to work:
Prims
There can be control prims with the following names:
"Up button" to elevate the barrel
"Down button" to depress the barrel
"Fire button" to fire the gun (mandatory)
"Auto button" to set/unset automatic firing
In addition, one child prim should be the gun itself, which optionally can be rotated up and down to provide variable elevation.
Root prim contents
The following should be in the contents of the root prim:
The vehicle gun script
A configuration notecard called "Gun config" (see below)
An object serving as the projectile
A sound file for the firing sound
Any movement scripts, such as the Vehicle script or Move & Rotate script, together with their support files
Gun config notecard
This notecard contains data for the Vehicle Gun script. An example follows:
Projectile = "Howitzer shell"
GunPrimName = "Barrel"
MuzzleVelocity = 15.0
Recoil = False
BarrelNaturalRot = <197.5, 0, 0>
BarrelTipPos = <0.0189, 2.342, -4.60>
DamageModel = Barrel, <0.0, 0.0, 0.829>, <-3.141, -1.6, 1.570>, <0.0, -0.589, 0.829>, <0.289, -1.136, -0.264>
DamageModel = Carriage, <-1.981, 0.0, 0.782>, <0.0, -1.570, -1.570>, <-1.981, 0.0, 0.700>, <-0.004, -1.500, -1.575>
FireSound = "howitzer_cannon_single_shot"
FireDelay = 3.0
The meaning of the fields is as follows:
Projectile - the name of the projectile object
GunPrimName - the name of the prim representing the gun
MuzzleVelocity - the speed at which the projectile is fired (arbitrary unit)
Recoil - (boolean) whether the gun should be forced back on firing
BarrelNaturalRot - the local rotation of the barrel when firing horizontally (typically to compensate for rotation inherited from a mesh model)
BarrelTipPos - the local position of the point where the projectile should be rezzed on firing
DamageModel - a line for each prim that's affected by damage modelling, followed by CSV representing: 1. the prim name; 2. undamaged pos; 3. undamaged rot; 4. damaged pos; 5. damaged rot. Rotations are in Euler radians
FireSound - the name of the sound file for the firing report
FireDelay - the delay (in seconds) after firing before the gun is usable again (ie reload time)
Comments (starting with "//") and blank lines are permitted.
Behaviour
When the fire button is clicked, a copy of the projectile object is rezzed at BarrelTipPos and propelled according to a vector derived from the following elements:
The gun object's Y axis (negative) - ie the default forward position of objects created by RezMela
BarrelNaturalRot, being a compensation for the barrel object's rotational deviation from the firing path
MuzzleVelocity, the speed of the projectile
On firing, the fire button turns orange and is unavailable for FireDelay seconds for "reloading", then turns green again and another shot may be fired.
Clicking the auto button will turn that button red, disable the fire prim, and start shooting automatically at FireDelay intervals until the auto button is clicked again.
Damage
If the object receives a message (sent using the osSendMessage() function by a projectile) consisting of "D" followed by the object's UUID, the DamageModel pos/rot data in the config notecard is applied, and the gun is disabled. A linked message with the integer value 58019400 is sent to child prims, to trigger smoke/flames/etc and to disable any vehicle script. All control prims are set to fully invisible.
In addition, a notecard is created in object inventory with the name "Remove this to reset". Removing that notecard resets the damage model, sends the linked message -58019400, renders the control prims visible and enables the gun for firing again. This latter functionality is to assist in testing.
Integration with vehicle script
Aside from the damage messages indicated above, the gun script will respond to a link message with integer value 391081100, which will cause the gun to fire. This is sent by the vehicle script when the Page Up key is pressed, enabling the gun to be fired from the keyboard.