- Home /
Firing Cooler
Hi, I tried to make a fire cooler, in which my hand gun shoots, the animation work, the raycast works, then it has to cool off for 0.5 seconds in order to re-fire. So I tried the following. I created a variable that is similar to my rate of fire and called it "ShootCooler, and then set it equal to each other after the fire function. But that didn't work. Here is my code without my trials:
NextFireAt = Time.time;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 1;
var AmmoInMag : float = 7;
var IsFullAuto = true;
static var AmmoLeft = 0;
static var ReloadTTime : float;
static var IsReloading = false;
private var CanFire = true;
var FireRate = 0.3;
var range = 100;
var DirtImpact : Transform;
var ConcImpact : Transform;
var WoodImpact : Transform;
var BulletBulletHole : Transform;
var WaterImpact : Transform;
var MetalImpact : Transform;
var BloodImpact : Transform;
private var NextFireAt : float = -1;
function Start () {
AmmoLeft = AmmoInMag;
ReloadTTime = ReloadTime;
}
function Update (){
if(IsReloading) return;
if(Time.time >= NextFireAt)
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);
}
if(hitTag == "Metal"){
Instantiate(MetalImpact,pos,rot);
Instantiate(BulletBulletHole,pos,rot);
}
if(hitTag == "Blood"){
Instantiate(BloodImpact,pos,rot);
}
}
}
}
function Reload(){
CanFire = false;
IsReloading = true;
BroadcastMessage("ReloadAnim");
yield WaitForSeconds (ReloadTime);
IsReloading = false;
CanFire = true;
AmmoLeft = AmmoInMag;
}
Answer by Seth-Bergman · Jul 20, 2012 at 04:18 AM
seems to me that's already handled in this script.. in the Fire function, with
if(CanFire && !IsReloading)
..(you don't need the "== true")
the value of the var FireRate would determine the rate of fire..
EDIT___________
The fire function only gets entered once every .3 seconds, due to the FireRate.. If you move the lines
yield WaitForSeconds(FireRate);
CanFire = true;
to the end, and put all your animation, sound, etc. calls above that, It will all only be called once every .3 seconds (or whatever the rate is set to).. isn't "Rate of fire" and "cool down time" the same thing?
as it stands now, the FireRate regulates both the cool down time between shots, AND the time it takes the bullet to reach its target.. so if that's what you intended, you could just add a second yield:
function Fire(){
if(CanFire && !IsReloading){
var hit : RaycastHit;
var fwd = Spawn.TransformDirection(Vector3.forward);
Debug.DrawRay(Spawn.position, fwd);
CanFire = false;
//initial animation code,sound etc. probably goes here
//if you wanted a delay before bullet hits, otherwise move next line to bottom
//yield WaitForSeconds(FireRate);
//This is what caused the delay, because it comes before the raycast etc.
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);
}
if(hitTag == "Metal"){
Instantiate(MetalImpact,pos,rot);
Instantiate(BulletBulletHole,pos,rot);
}
if(hitTag == "Blood"){
Instantiate(BloodImpact,pos,rot);
}
}
//could have "cool down code" here, if you need..
yield WaitForSeconds(coolDownRate);
CanFire = true;
}
}
the rate of fire works fine. I just want to add cool off to the weapon. A period of time that takes to redo the fire function and eventually the fire animation, sound, and raycast effect.
The fire function only gets entered once every .3 seconds, due to the FireRate.. If you move the lines
yield WaitForSeconds(FireRate); CanFire = true;
to the end, and put all your animation, sound, etc. calls above that, It will all only be called once every .3 seconds (or whatever the rate is set to).. isn't "Rate of fire" and "cool down time" the same thing?
edited my answer above, think that's what you're after..
already was judging by the old code.. anyway, just delete the line yield WaitForSeconds(FireRate);.. I'll update the code above..
now ins$$anonymous$$d we're using "coolDownRate", but it doesn't matter either way, it's still a float, you could just replace that with FireRate
Your answer
Follow this Question
Related Questions
Controlled Automatic Fire Rate (RayCasting) 2 Answers
Start timer with Input 2 Answers
Gun Script glich? 1 Answer
Is there a script out there that actully works for shooting bullets? 2 Answers
Shooting question 1 Answer