Introduction
The projectile script resides inside the projectile object fired by the Vehicle gun script. It controls the behaviour of the projectile after firing, including creating an explosion on impact and communicating damage data to affected targets. This page also covers the associated Explosion script.
Construction
Due to problems trying to make the projectile turn in flight as it describes its trajectory, it is recommended that a simple sphere be used so that its attitude in flight is not apparent; the object does not turn at all.
The root (or only) prim should contain the following:
Texture (only texture in contents) for "smoke" trail left by projectile
Explosion sound - named "explosion"
Explosion prim (see below)
Projectile sound (optional) - named "projectile", looped, for sound made by projectile in flight
The projectile object must be set to physical when placed in the gun contents.
Operation
When the projectile is first launched, it starts to emit particles based on the texture in inventory, and if a sound is present named "projectile", that sound is looped while in flight.
On collision with an object, the shell will "explode", and the following will occur:
The projectile loses its physical status, and becomes tiny and invisible
If the projectile hasn't travelled further than a set minumum distance, it will die silently at this point
The projectile sound stops, and an explosion sound is triggered
The explosion prim is rezzed
The script searches for nearby NPCs and scripted objects, sending messages to them to indicate their destruction
After a five-second delay to allow the above to happen, the object dies
Dealing destruction
On impact, any NPCs and scripted objects (to a maximum dictated by llSensor()'s capabilities) are messaged to indicate that they should take damage.
This takes the form of osMessageObject() messages to objects, and llRegionSay() messages on channel 29904047 to NPCs (the latter because the NPC attachments' UUIDs are not knowable; remember that attachments can receive chat messages sent to the owners). In both cases, the message contents are a "D" followed by the UUID of the object or NPC.
Typically, an object will simulate destruction, and an NPC will simulate death.
Explosion prim
This is a small, invisible prim inside the projectile object whose purpose is to provide the explosion particle effect and then die. It contains the explosion script, and the texture for the explosion.
These are in a separate prim so that the explosion appears to occur at the point of impact, even if the projectile bounces and/or continues travelling for a distance before disappearing.
Miscellaneous notes
If the projectile is rezzed from inventory rather than by the gun script, it will turn non-physical to prevent it bouncing and rolling away. It must be set to physical again before taking back into inventory if the intention is to use it in the gun.
The projectile sets its own temp-on-rez and die-at-edge status to ensure it doesn't survive. It also kills itself after 20 seconds just in case.
The minimum distance prevents the shell from detonating in cases where the simulator appears to fail to provide impetus to the projectile on rezzing
On occasion, collisions fail to happen, and the projectile will pass through the target