- Home /
My gun can't shoot after some adjustments to the script
So I've asked a simmer question like this before, but the cause of this one (I know for a fact) is different from this one.
Anyway, I wanted to keep track off how much ammo I have on my gun so I made a few variables and added of few stuff to the Function update. once those parts were added, it made my gun not fire. Here is the script: #pragma strict
var bullet : GameObject;
private var bulletsPerSecond = 20.0;
private var shooting = false;
private var Allammo : int = 320;
var ammo : int = 40;
var sound: AudioClip;
var Gun : GameObject;
var Subammo : int = 1;
function Start()
{
InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
}
function Shoot()
{
if (!shooting) return;
var go = Instantiate(bullet, transform.position, transform.rotation);
go.rigidbody.AddRelativeForce(Vector3.forward * 1000.0);
ammo = (ammo - 1);
audio.PlayOneShot(sound);
SendMessage("Ammo_Subtract", Subammo, SendMessageOptions.DontRequireReceiver);
}
function Update()
{
shooting = false;
if(Input.GetAxis("Fire1"))
{
shooting = true;
}
if (ammo == 0)
{
Allammo = (Allammo - 40);
ammo = 40;
SendMessage("All_ammoSub", ammo, SendMessageOptions.DontRequireReceiver);
}
if (Allammo == 0);
{
shooting = false;
}
}
This is on Unity 4.6, and these are the added parts that caused it not to shoot:
if (ammo == 0)
{
Allammo = (Allammo - 40);
ammo = 40;
SendMessage("All_ammoSub", ammo, SendMessageOptions.DontRequireReceiver);
}
if (Allammo == 0);
{
shooting = false;
}
Ignore the SendMessage part of that (unless that is affecting it which i highly doubt). Anyway, if you find anything wrong with the script, please let me know, thanks!
i think the-
if (Allammo == 0);
{
shooting = false;
}
is making the problem but don't know for sure
Answer by DoTA_KAMIKADzE · Apr 09, 2015 at 12:01 AM
This part (line 25):
Input.GetAxis("Fire1")
Returns float value and so it can't be used in if statement and also it should give you an error...change it to something like:
if(Input.GetButtonDown("Fire1"))
P.S. Though if it is an axis not a button then check something like:
if(Input.GetAxis("Fire1") > 0)
That will work.
P.P.S. You can also just check if it "!= 0" if any direction is fine for fire.
P.P.P.S. Though your code is still pretty badly designed for your repeating function, how about rewriting it like this?:
if (Allammo > 0)
{
if (ammo > 0)
{
if (Input.GetButtonDown("Fire1"))
{
shooting = true;
}
if (Input.GetButtonUp("Fire1"))
{
shooting = false;
}
}
else
{
shooting = false;
Allammo = (Allammo - 40);
ammo = 40;
SendMessage("All_ammoSub", ammo, SendMessageOptions.DontRequireReceiver);
}
}
else shooting = false;
I think that will suit you more.
P.P.P.P.S. Ah yeah also completely forgot:
Change line #15 to:
if (!shooting || ammo < 1) return;
Just because your FPS might be low to prevent few more bullets going off ))
I edited the script like you said but nothing changed, no sound playing, no bullets firing.
P.S. I've added and tried everything you have said but still getting the same problem :(
Remove all your code under Update function and paste the one in my "P.P.P.S." section I guess that is the behavior you expect, if not then let me know.
Your answer
Follow this Question
Related Questions
im having trouble with raycast shooting script 0 Answers
ammo counter, counts wrong 1 Answer
how do i use touch.Position 1 Answer
expecting (, found 'Update' and unexpected char: 0x200B. 2 Answers