- Home /
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");
}
}
Answer by Berenger · Feb 21, 2012 at 10:30 PM
empty, once set to true, is never set back to false on reload.
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?
I edited your script. The edit on this website takes a few hours for some reasons, you should see it tomorrow.
Your answer
Follow this Question
Related Questions
Shooting. Bullet floats and sprays 1 Answer
Bullet Drop With Raycast 4 Answers
Need a script for a gun that shoots bullet using raycast 2 Answers
Bullet Hole not inline with sight?(Center of Screen) 1 Answer
C# ShootController() 3 Answers