- Home /
Gun Script glich?
I tried to put a reload animation but that failed so I removed it and I ended up with something weird where the gun would fire once every frame and would not respond to any input and also would not stop when it ran out of ammo. help? #pragma strict
var Effect : Transform;
var TheDammage = 100;
var forward;
var MAmmo = 30;
static var ToatalAmmo = 90;
var offsetY : float = 40;
var sizeX : float = 100;
var sizeY : float = 40;
var MagSize = 0;
var ReloadSound : AudioClip;
var ShootingSound : AudioClip;
var IsSniper = false;
var IsG36 = false;
// Checking what weapon is what.
function Start (){
if (IsG36 == true){
MAmmo = 30;
ToatalAmmo = 90;
MagSize = 30;
}
if (IsSniper == true){
MAmmo = 7;
ToatalAmmo = 28;
MagSize = 7;
}
}
//Adding Ammo GUI
function OnGUI () {
GUI.Box (new Rect (Screen.width/2-sizeX/2, offsetY, sizeX, sizeY), "Ammo " + MAmmo + "/" + ToatalAmmo);
}
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
//Control
if (Input.GetMouseButtonDown(0))
{
}
if (Physics.Raycast (ray, hit, 100))
{
//Reloading (WIP)
if(MAmmo < 1){
ToatalAmmo -= MagSize;
MAmmo += MagSize;
}
//Subtrating From Ammo
MAmmo --;
//Firing gun
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
audio.Play();
particleSystem.Play();
}
}
Answer by matheusrma · May 27, 2014 at 04:21 AM
Hey Facecakes,
Are you a coder? Or just experimenting with this script. I would recommend you start reading about C# basics.
Now about your problem:
The gun is firing every frame cause of the closing braces at line 40. It is closing the if scope leaving the code that should be inside, outside. Remove it and this behaviour should stop. This fix the lack of input too.
About it not stoping after ammo ends, make the if on line 38 look like this
if (Input.GetMouseButtonDown(0) && ToatalAmmo > 0)
But, as I said, I recommend you try to understand why the changes I proposed worked =)
Your answer
![](https://koobas.hobune.stream/wayback/20220613145956im_/https://answers.unity.com/themes/thub/images/avi.jpg)