dim3 Forum

Full Version: Bullet Casings Tutorial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Bullet Casings Tutorial

By Alexander Smith

[Image: casingpic.jpg]

In this Tutorial, I will explain, and show you how to make your in-game weapons eject casings for each shot. Not only does this look cool, but it makes the game more realistic.

For this tutorial, you need to have some very basic scripting knowledge, as well as an idea on how to use the Animator application. Got that? Let's start!

Part One—Setting up the Weapon

This step is really the easiest, but it's the whole foundation of the tutorial. In order for the weapon to eject casings, the game has to know the location of the ejection area. This is easily done, by placing a bone in the spot where you want the casings to eject.

Steps:

#1: Open the weapon model in Animator.
#2: Create a bone, and name it "ejct" this is the name of the ejection port, "ejct" for short.
#3: Assuming you understand how to use Animator, move the bone to the location of the ejection port, or move it to where the casing should eject. It can be anywhere.
#4: Connect the "ejct" bone to the main weapon bone, by making the main bone be the "ejct" bone's parent. Do this because otherwise, the bullet will eject in the air from no apparent point. It HAS to be connected to the main weapon bone.
#5: Save the weapon model, and quit Animator. Your weapon is setup and you will not have to return to the Animator.

Part Two—Changing the Weapon Script

Now that the model is ready, we now have to prepare the weapon script. All you have to do is add a couple of lines of code, and it's done, we keep this as simple as possible to keep frame-rate high, and code content low.

Steps:

#1: Open the weapon script.
#2: Add the following code to the weaponConstruct function.
Under the Projectile Code Area add:
Code:
    weap.projectile.add('casing');
    weap.projectile.fireBoneTag='ejct';

#3: Now, scroll down the the weaponFire function. Add this right below the (weap.projectile.spawn) command for firing of the bullet.
Code:
    weap.projectile.spawnFromWeaponBoneOffsetAngle('casing',25,0,45);
This little bit is telling the script to fire the casing projectile (we'll get to that) at x,z,y angles. The current angles are pretty good, but you can adjust them as you need. Now the weapon script is finished, and we can move on to the last part.

Part Three—Adding the Casing Script

This part is the easiest, simple download the ZIP file below, and add the included "casing.js" file to the Data>>Scripts>>Projectiles folder. The script looks like this:
Code:
// ***********************************************************
//
// PROJECTILE: bullet casing
//
// ***********************************************************
//
// casing construction
//

function casingConstruct(proj)
{
        // model

    proj.model.on=true;
    proj.model.name="BULLETCASING";
    proj.model.spin.x=5;
    proj.model.spin.z=1;
    proj.model.spin.y=1;
    proj.action.collision=false;

        // speed

    proj.speed.speed=45;
    proj.speed.deceleration=5;
    proj.speed.decelerationMinSpeed=20;

        // automatic functions

    proj.action.autoHitTick=2000;        // auto-destroys after 2 seconds    

        // size and weight

    proj.size.x=0;
    proj.size.z=0;
    proj.size.y=0;
    proj.size.weight=150;
}

//
// casing destroy
//

function casingHit(proj,tick)
{
    proj.action.destroy();
}

//
// events
//

function event(proj,mainEvent,subEvent,eventId,tick)

{

    switch (mainEvent) {

        case DIM3_EVENT_CONSTRUCT:
            casingConstruct(proj);
            return;

        case DIM3_EVENT_HIT:
            casingHit(proj,tick);
            return;
    }
}

The only problem with this script is that the construct function controls the spin. On an automatic weapon, this may be a problem, as the casing will not spin randomly, though on a semi-auto, it looks like it does. If you want to add random spinning, you'll have to do that yourself, or wait for an update to this tutorial.

**THE ZIP FILE**

DOWNLOAD: Here

**THE ZIP FILE**

Zip file contains the casing model, and the casing script. Enjoy. Smile

I hope you liked this tutorial, please send any comments to me by email (I hate PMs) or rep me, haha (isn't that cheap).

Email: alexandersmith*at*coldfusiongames.com
Reference URL's