x


Reload when press "r"?

Basicly it kind of works but it reloads when you empty a clip.... or if are in the process of shooting (im holding down the mouse button) and i press "r" it will reload but if i just push "r" (even with only half a magazine or something) it wont reload. i have tried moving the "if(Input.GetKeyDown("r")) Reload();" to different places but it doesnt help. i've also tried making new functions. how come this happens?

 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;
    var reloadSound : AudioClip;

    private var hitParticles : ParticleEmitter;
    var muzzleFlash : Renderer;

    private var clipsLeft : int = 0;
    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;
        clipsLeft = clips;

    }



    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();

        if(Input.GetKeyDown("r"))
        Reload();


        }




    function Reload () 
    {



    audio.PlayOneShot(reloadSound, 1.0 / audio.volume);

    animation.CrossFade ("Reload right");

        // Wait for reload time first - then add more bullets!




        yield WaitForSeconds(reloadTime);



        if (clipsLeft > 0) {
            clipsLeft--;
            bulletsLeft = bulletsPerClip;



        }
        }







    function GetBulletsLeft () {
        return bulletsLeft;
    }

    function GetClipsLeft (){
        return clipsLeft;
        }








UPDATE***********************************************************
New Script(SLIGHTLy) modified added new function for Input.GetKeyDown.....

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;
var reloadSound : AudioClip;

private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;

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


function Update(){

if (Input.GetKeyDown("r"))
    Reload();
    }




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;
    clipsLeft = clips;

}

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 () 
{



audio.PlayOneShot(reloadSound, 1.0 / audio.volume);

animation.CrossFade ("Reload right");

    // Wait for reload time first - then add more bullets!




    yield WaitForSeconds(reloadTime);



    if (clipsLeft > 0) {
        clipsLeft--;
        bulletsLeft = bulletsPerClip;



    }
    }







function GetBulletsLeft () {
    return bulletsLeft;
}

function GetClipsLeft (){
    return clipsLeft;
    }
more ▼

asked Feb 07 '11 at 12:43 AM

BrettRiet gravatar image

BrettRiet
49 12 12 19

have you try putting in Update function??

Feb 07 '11 at 01:02 AM Uriel_96

i have it doesn't work anywhere or way??

Feb 07 '11 at 01:03 AM BrettRiet
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It appears that the only place Reload() is called is from within FireOneShot(), so if you are not firing it is impossible to even get to the reload code. Move the check for the 'r' key to Update().

more ▼

answered Feb 07 '11 at 02:03 AM

Molix gravatar image

Molix
4.8k 17 27 66

sorry i;m not very good at scripting... do you mean put "if(Input.GetKeyDown("r")) Reload();" to "function LateUpdate(){" or do i make a new "function"?

Feb 08 '11 at 01:21 AM BrettRiet

Yes, those two lines, but put them in Update(), rather than LateUpdate.

Feb 08 '11 at 04:12 AM Molix

no matter how i put it,it doesn't work? i tried putting the lines in a new function i created "Update()" but it still has to be shooting while i press are there is no way i can put it in?

Feb 08 '11 at 05:33 AM BrettRiet

is there some way i can say like " && shooting = false" or something along those lines...

Feb 08 '11 at 05:34 AM BrettRiet

You should probably put up your new code.

Feb 08 '11 at 04:16 PM Molix
(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:

x3570
x86
x19

asked: Feb 07 '11 at 12:43 AM

Seen: 1174 times

Last Updated: Feb 08 '11 at 11:41 PM