x


Troubles With A Shoot Script

Okay, so I am relativity new to scripting in Unity3d but not new to the program its self. I have a shoot script that I am having some difficulties with. If you apply the script to the first person controller (along with another script I will include), everything works as it is supposed to. Except for the fact that you can't reload until you run out of ammo(this includes that you can't reload when you have ammo left). When you run out of ammo you can reload just fine, even when you have ammo left. Once you run out of ammo the first time, you get a label that says "RELOAD". This label won't go away at all, not even once you have reloaded("r"). I found that those two things were a problem, and I have worked to my best efforts to try and figure them out. Some help would be greatly appreciated. If something didn't make sense, please tell me and I will try to explain it more clearly. Thanks!

Scripts: Firearm:

var fireRate : float = 0.3;
private var nextFire : float = 2.0;
var Ammo : int = 30;
var gunfire : AudioClip;
var reload : AudioClip;
var empty : boolean = false;
var Projectile : Transform;

function Update () 
{
    empty = Ammo <= 0;
    if(Input.GetButton("Fire1")&&Time.time > nextFire)
        if(!empty)
            Fire();    

    if(Input.GetKeyUp("r"))
        if(empty)
            Reload();   
}

function Fire(){    
    audio.PlayOneShot(gunfire);
       nextFire = Time.time + fireRate;

       var shot = Instantiate(Projectile, transform.position, Quaternion.identity);
       shot.rigidbody.AddForce (transform.forward * 3000);
       Ammo--;
}

function Reload(){      
    audio.PlayOneShot(reload);
    Ammo = 30;     
}

function OnGUI(){    
    GUI.Box(Rect(Screen.width - 100, 20, 100, 80), "");
    GUI.Label(Rect(Screen.width - 100, 35, 90, 20), "Ammo:" + Ammo);
    GUI.Label(Rect(Screen.width - 100, 20, 90, 20), "Health" + PlayerHealth.currentHealth);

    if(empty){
    GUI.contentColor = Color.red;
    GUI.Label(Rect(Screen.width - 100, 50, 90, 20), "RELOAD");         
       }
    }

PlayerHealth(Make sure the script is called this): var fireRate : float = 0.3; private var nextFire : float = 2.0; var Ammo : int = 30; var gunfire : AudioClip; var reload : AudioClip; var empty : boolean = false; var Projectile : Transform;

function Update () 
{
    empty = Ammo <= 0;
    if(Input.GetButton("Fire1")&&Time.time > nextFire)
        if(!empty)
            Fire();    

    if(Input.GetKeyUp("r"))
        if(empty)
            Reload();   
}

function Fire(){    
    audio.PlayOneShot(gunfire);
       nextFire = Time.time + fireRate;

       var shot = Instantiate(Projectile, transform.position, Quaternion.identity);
       shot.rigidbody.AddForce (transform.forward * 3000);
       Ammo--;
}

function Reload(){      
    audio.PlayOneShot(reload);
    Ammo = 30;     
}

function OnGUI(){    
    GUI.Box(Rect(Screen.width - 100, 20, 100, 80), "");
    GUI.Label(Rect(Screen.width - 100, 35, 90, 20), "Ammo:" + Ammo);
    GUI.Label(Rect(Screen.width - 100, 20, 90, 20), "Health" + PlayerHealth.currentHealth);

    if(empty){
    GUI.contentColor = Color.red;
    GUI.Label(Rect(Screen.width - 100, 50, 90, 20), "RELOAD");         
       }
    }
more ▼

asked Feb 21 '12 at 08:40 AM

Major gravatar image

Major
63 4 9 15

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

1 answer: sort voted first

empty, once set to true, is never set back to false on reload.

more ▼

answered Feb 21 '12 at 10:30 PM

Berenger gravatar image

Berenger
11k 12 19 53

Right so how do I make it set back to false once I reload?

Feb 23 '12 at 03:08 AM Major

add empty = false; in Reload function.

Feb 23 '12 at 03:18 AM Berenger

Okay I will thanks!

Feb 23 '12 at 03:52 AM Major

I got an error. I put empty=false; at the end of the function that was in the brakets, is there another spot that i'm supposed to put it?

Feb 24 '12 at 04:43 AM Major

I edited your script. The edit on this website takes a few hours for some reasons, you should see it tomorrow.

Feb 24 '12 at 05:06 AM Berenger
(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:

x3334
x446
x440
x329
x308

asked: Feb 21 '12 at 08:40 AM

Seen: 815 times

Last Updated: Feb 27 '12 at 01:48 AM