x


Some issues with weapon switching and reloading?

Hi, I used the Machinegun.js script from the FPS tutorial :

var range = 100.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 40;
var clips = 20;
var reloadTime = 0.5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;

private var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;

function Start () {
    hitParticles = GetComponentInChildren(ParticleEmitter);

    // We don't want to emit particles all the time, only when we hit something.
    if (hitParticles)
        hitParticles.emit = false;
    bulletsLeft = bulletsPerClip;
}

function LateUpdate() {
    if (muzzleFlash) {
        // We shot this frame, enable the muzzle flash
        if (m_LastFrameShot == Time.frameCount) {
            muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
            muzzleFlash.enabled = true;

            if (audio) {
                if (!audio.isPlaying)
                    audio.Play();
                audio.loop = true;
            }
        } else {
        // We didn't, disable the muzzle flash
            muzzleFlash.enabled = false;
            enabled = false;

            // Play sound
            if (audio)
            {
                audio.loop = false;
            }
        }
    }
}

function Fire () {
    if (bulletsLeft == 0)
        return;

    // If there is more than one bullet between the last and this frame
    // Reset the nextFireTime
    if (Time.time - fireRate > nextFireTime)
        nextFireTime = Time.time - Time.deltaTime;

    // Keep firing until we used up the fire time
    while( nextFireTime < Time.time && bulletsLeft != 0) {
        FireOneShot();
        nextFireTime += fireRate;
    }
}

function FireOneShot () {
    var direction = transform.TransformDirection(Vector3.forward);
    var hit : RaycastHit;

    // Did we hit anything?
    if (Physics.Raycast (transform.position, direction, hit, range)) {
        // Apply a force to the rigidbody we hit
        if (hit.rigidbody)
            hit.rigidbody.AddForceAtPosition(force * direction, hit.point);

        // Place the particle system for spawing out of place where we hit the surface!
        // And spawn a couple of particles
        if (hitParticles) {
            hitParticles.transform.position = hit.point;
            hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
            hitParticles.Emit();
        }

        // Send a damage message to the hit object          
        hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    }

    bulletsLeft--;

    // Register that we shot this frame,
    // so that the LateUpdate function enabled the muzzleflash renderer for one frame
    m_LastFrameShot = Time.frameCount;
    enabled = true;

    // Reload gun in reload Time        
    if (bulletsLeft == 0)
        Reload();           
}

function Reload () {

    // Wait for reload time first - then add more bullets!
    yield WaitForSeconds(reloadTime);

    // We have a clip left reload
    if (clips > 0) {
        clips--;
        bulletsLeft = bulletsPerClip;
    }
}

function GetBulletsLeft () {
    return bulletsLeft;

and the weapon switching script, which is :

function Start () {
    // Select the first weapon
    SelectWeapon(0);
}

function Update () {
    // Did the user press fire?
    if (Input.GetButton ("Fire1"))
        BroadcastMessage("Fire");

    if (Input.GetKeyDown("1")) {
        SelectWeapon(0);
    }   
    else if (Input.GetKeyDown("2")) {
        SelectWeapon(1);
    }   
}

function SelectWeapon (index : int) {
    for (var i=0;i<transform.childCount;i++)    {
        // Activate the selected weapon
        if (i == index)
            transform.GetChild(i).gameObject.SetActiveRecursively(true);
        // Deactivate all other weapons
        else
            transform.GetChild(i).gameObject.SetActiveRecursively(false);
    }
}

     }

Now, when i switch weapons while reloading (during the time span) and switch back to the machinegun, the machinegun wont reload if there is no bullet available. Can anyone tell me how do i solve this? I tried many ways, like putting an "if the gun is reloading, switch weapons = false" but cant seem to shape it. I also did searched around the forums and found this thread : link

but theres no answer too... can anyone help me with this script? Thanks!

more ▼

asked Dec 27 '10 at 10:28 AM

user-3061 (yahoo) gravatar image

user-3061 (yahoo)
126 29 29 37

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Check the Edit->Project Settings->Input. YOu have to have the keys 1 and 2 enstablished there. just go to the tutorial and see the input from there.

[b]Another logic[/b]

I don't know about FPS tutorial but what i would do is having the two guns at (and at the Hiereancy inside) the FPS prefab.The second gun i would make it invisible and then i would write something like : When User press button 2 make 1st gun invisible and make 2nd gun visible.

That was just a logic that you can base on the script you have from the Tutorial

more ▼

answered Dec 27 '10 at 11:58 AM

NickCh gravatar image

NickCh
170 6 8 13

sorry, you completely misunderstood my question... please read more carefully before answering it, my problem is with the "Reload" and "ChangeWeapons" function.Thanks for the reply tho.

Dec 27 '10 at 12:28 PM user-3061 (yahoo)
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5053
x3443
x1171
x445
x84

asked: Dec 27 '10 at 10:28 AM

Seen: 1018 times

Last Updated: Dec 27 '10 at 10:28 AM