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;
}