- Home /
Gun shooting and ammo problem
I tried to make my script shoot a projectile from the gun and then that would subtract a number from a GUI text and then have it where I could run over a game object and it would add ammo to my gun and add a number to the GUI text. could someone tell me what part I need to fix.
#pragma strict
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 8;
var shootSound : AudioClip;
var ammoAmount : int = 50;
public var ammo : int = 0;
public var GUIammo : GUIText;
function Update()
{
if(Input.GetButtonDown("Fire1") && shots < maxShots)
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
audio.PlayOneShot(shootSound);
shots++;
}
}
function Update()
{
function OnTriggerStay(Collider);
{
if (tag == "ammo")
{
Destroy(gameObject);
ammo += 1;
shots -= 1;
GUIammo.text = ammo.ToString();
}
}
}
no it just doesn't do the adding ammo thing I wanted, sorry I wasn't exact
BTW sorry I put that on answer. I spend most of time unity+forums. Lol
Well it depends. I just add physics and when the body falls over I call it dead and disable scripts.
yes it does get destroyed but i think it's because i added un needed things in that script i got it working now, thanks everyone!
Answer by Komak57 · Jan 20, 2014 at 10:08 PM
Usually safer in this case to use OnGUI() to render the clip information:
void OnGUI() {
Transform target = transform;
while (target.parent != null) {
target = target.parent;
if (target.name == "Player") {
GUI.Box(new Rect(10,Screen.height-60,ClipSize*20+10,50), new Texture());
if (_ReloadSpeed > 0) {
GUI.Label(new Rect(15,Screen.height-60,70,30), ".../"+(TotalAmmo));
} else {
GUI.Label(new Rect(15,Screen.height-60,70,30), Ammo+"/"+(TotalAmmo));
}
for(int i = 0; i < Ammo; i++) {
GUI.DrawTexture(new Rect(i*20+15,Screen.height-30,20,20), ClipTexture);
}
return;
}
}
}
Also, I've noticed that adding a script to the bullet to ray-cast and teleport has a much better collision performance. As far as ammo goes, you need to be more specific.
You don't want to fire while reloading.
You don't want to fire when your clip is empty.
You don't want to reload when you're out of ammo.
Once you've jotted down all of your parameters, you can just throw looted ammo into the ammo variable, ensuring that there is a maximum if you so desire (most games do) etc etc.
As far as your looting problem, I have another script to act as an equipment manager. This script is initialized within another script attached to the parent object, though there's many ways to do this differently. Said object runs a 'void OnTriggerEnter(Collider c)' to decide if the player has come into auto-loot range, and then manages the ammo from the ground weapon to the players weapon. Get creative!
public int TransferAmmo(GameObject obj) {
GunMechanics loot = obj.GetComponent<GunMechanics>(); // firing, reloading, etc
// Scan for item
foreach(Transform child in handle) {
if (child.name == obj.name) {
GunMechanics equip = child.GetComponent<GunMechanics>();
int AmmoToTransfer = loot.TotalAmmo;
// cap to full ammo
if (AmmoToTransfer + equip.TotalAmmo > equip.MaxAmmo)
AmmoToTransfer = equip.MaxAmmo - equip.TotalAmmo;
equip.TotalAmmo += AmmoToTransfer;
if (AmmoToTransfer > 0)
Debug.Log("Obtained "+AmmoToTransfer+"x "+equip.Bullet.name+" bullets.");
return AmmoToTransfer;
}
}
return 0;
}
Your answer
Follow this Question
Related Questions
Repeatedly Getting Errors On An Ammo Script 3 Answers
In Game Animation 2 Answers
GUI Problem 2 Answers
Javascript code not working plz help 1 Answer
2D shooting problem please help! 2 Answers