- Home /
Help in change the rate of fire.
So I created a code for my gun with reload, rate of fire, range, and fire variables. However, when I reload, the rate of fire goes really high for some reason. Example: http://www.youtube.com/watch?v=PuiOOURL250.
My Code:
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag : float = 30;
var IsFullAuto = true;
static var AmmoLeft = 0;
static var ReloadTTime : float;
static var IsReloading = false;
private var CanFire = true;
var FireRate = 0.1;
var range = 100;
var DirtImpact : Transform;
var ConcImpact : Transform;
var WoodImpact : Transform;
var BulletBulletHole : Transform;
var WaterImpact : Transform;
var MetalImpact : Transform;
function Start () {
AmmoLeft = AmmoInMag;
ReloadTTime = ReloadTime;
}
function Update (){
if (Input.GetKeyDown(KeyCode.R))
{
BroadcastMessage("ReloadAnim");
Reload();
}
if(IsFullAuto == false){
if(Input. GetButtonDown("Fire1")){
if(AmmoLeft > 0){
BroadcastMessage("FireAnim");
Fire();
}
}
}
else{
if(Input. GetButton("Fire1")){
if(AmmoLeft > 0){
BroadcastMessage("FireAnim");
Fire();
}
}
}
if(AmmoLeft == 0)
{
Reload();
}
if(AmmoLeft < 0){
AmmoLeft = 0;
}
}
function Fire(){
if(CanFire == true && IsReloading == false){
var hit : RaycastHit;
var fwd = Spawn.TransformDirection(Vector3.forward);
Debug.DrawRay(Spawn.position, fwd);
CanFire = false;
yield WaitForSeconds(FireRate);
CanFire = true;
AmmoLeft -= 1;
audio.Play();
if(Physics.Raycast(Spawn.position,fwd,hit,range)){
var hitTag = hit.collider.tag;
var pos = hit.point+0.01*hit.normal;
var rot = Quaternion.FromToRotation(Vector3.forward, hit.normal);
if(hitTag == "Dirt"){
Instantiate(DirtImpact,pos,rot);
Instantiate(BulletBulletHole,pos,rot);
}
if(hitTag == "Concrete"){
Instantiate(ConcImpact,pos,rot);
Instantiate(BulletBulletHole,pos,rot);
}
if(hitTag == "Wood"){
Instantiate(WoodImpact,pos,rot);
Instantiate(BulletBulletHole,pos,rot);
}
if(hitTag == "Water"){
Instantiate(WaterImpact,pos,rot);
Instantiate(BulletBulletHole,pos,rot);
}
if(hitTag == "Metal"){
Instantiate(MetalImpact,pos,rot);
Instantiate(BulletBulletHole,pos,rot);
}
}
}
}
function Reload(){
CanFire = false;
IsReloading = true;
BroadcastMessage("ReloadAnim");
yield WaitForSeconds (ReloadTime);
IsReloading = false;
CanFire = true;
AmmoLeft = AmmoInMag;
}
The indentation is still all over the place, please try editing it again.
Id this your script or an edited script you've downloaded?
Hmmm the indentation of your code is not consistent with braces ending at the same level of indentation on adjacent lines and it not being clear where one area of scope begins and ends?
Answer by Bovine · Jul 17, 2012 at 05:29 AM
I'm not sure why Fire() is a coroutine - you seem to be using CanFire as a lock around firing, but you're firing many coroutines potentially that will wait and then fire. There's also no check after your WaitForSeconds() to see whether there's any ammo left - you also deplete the ammo after the wait which I feel sure is going to lead to problems.
I'm suspicious as to why you need the check if AmmoLeft < 0 - i suspect your ammo keeps going negative and you've thrown this in to fix it, but this is something of a bandaid of the real problem.
Personally I would take the locking out, other than around reloading and replace the coroutine with a simple check for elapsed time, so you might have a variable so:
private var NextFireAt : float = -1;
Which in Awake() you would set:
NextFireAt = Time.time;
To capture when the player can next fire, then in your Update()
if(Time.time >= NextFireAt)
{
// do your check whether the key is down/has been hit here
// you could also check for ammo FIRST before
// checking IsFullAuto and for any key presses
}
And then get rid of the coroutine in Fire() as you don't need a wait, your Update() is now limiting the rate of fire, but when you enter the Fire() routine you need to set:
NextFireAt = Time.time + FireRate;
In Update() at the top you can probably then:
if(IsReloading) return;
Hopefully that helps.
Apologies if the code isn't quite right as I'm a C# bod really, but it should be obvious what I am suggesting I hope!
Your answer
Follow this Question
Related Questions
Realistic reloading system 1 Answer
Fooling Rate Of Fire C# 1 Answer
Creating fire particle in code 1 Answer
separate script to reload. 0 Answers
rapid movement back and forth 1 Answer