- Home /
Muzzle Flash turning on and off even when not firing
Hey everyone! My muzzle flash and all of the lights in the scene keep on turning off and on even when I am not firing! Can anyone help? BTW This is my Ray Shooting script so far:
#pragma strict
var Range : float = 1000;
var Force : float = 1000;
var Clips : int = 20;
var BulletPerClip : int = 36;
var ReloadTime : float = 4.6;
var BulletsLeft : int = 0;
var ShootTimer : float = 0;
var ShootCooler : float = 0.09;
var Damage : int = 20;
var HitParticles : ParticleEmitter;
var muzzleFlash : ParticleEmitter;
var muzzleCooler : float = 0.9;
var muzzleFlashTimer : float = 0;
var MuzzleSpeed : int = 200000;
var light1 : GameObject;
var light2 : GameObject;
var light3 : GameObject;
public var ShootAudio : AudioClip;
public var ReloadAudio : AudioClip;
function Start () {
BulletsLeft = BulletPerClip;
muzzleFlash.emit = false;
HitParticles.emit = false;
}
function Update () {
if(muzzleFlashTimer > 0){
muzzleFlashTimer -= Time.deltaTime;
MuzzleFlashShow();
}
if(muzzleFlashTimer < 0){
muzzleFlashTimer = 0;
}
if(Input.GetMouseButton(0) && BulletsLeft){
if(ShootTimer == 0){
PlayShootAudio();
RayShoot();
ShootTimer = ShootCooler;
}}
if(muzzleFlashTimer == 0){
muzzleFlashTimer = muzzleCooler;
MuzzleFlashShow();
}
if(ShootTimer > 0){
ShootTimer -= Time.deltaTime;
}
if(ShootTimer < 0){
ShootTimer = 0;
}
}
function MuzzleFlashShow (){
if(muzzleFlashTimer > 0){
muzzleFlash.emit = false;
light1.active = false;
light2.active = false;
light3.active = false;
}
if(muzzleFlashTimer == muzzleCooler){
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * MuzzleSpeed , Vector3.forward);
muzzleFlash.emit = true;
light1.active = true;
light2.active = true;
light3.active = true;
}
}
function RayShoot () {
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position , DirectionRay * Range , Color.red);
if(Physics.Raycast(transform.position , DirectionRay , Hit, Range)) {
if(Hit.rigidbody) {
if(HitParticles){
HitParticles.transform.position = Hit.point;
HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
HitParticles.Emit();
}
Hit.rigidbody.AddForceAtPosition( DirectionRay * Force , Hit.point);
Hit.collider.SendMessageUpwards("ApplyDamage" , Damage, SendMessageOptions.DontRequireReceiver);
}
}
BulletsLeft --;
if(BulletsLeft < 0){
BulletsLeft = 0;
}
if(BulletsLeft == 0){
Reload();
}
}
function Reload (){
PlayReloadAudio();
yield WaitForSeconds(ReloadTime);
if(Clips > 0){
BulletsLeft = BulletPerClip;
}
}
function PlayShootAudio () {
audio.PlayOneShot(ShootAudio);
}
function PlayReloadAudio () {
audio.PlayOneShot(ReloadAudio);
}
Answer by ifisch · Jul 10, 2013 at 05:57 PM
It seems like the muzzleflash firing on and off is the behavior you intended to program.
The timer starts at 0.9s, decrements until it hits zero, and then is reset to 0.9s.
During the frame where the timer equals 0.9, you're telling your particles to emit. All values below 0.9s, you're telling the particles not to emit. So it seems to me that particles should be emitting for a single frame, every 0.9 seconds.
Does that answer your question?
@ifisch Thanks man! What I wanted it to do, is that everytime you left click the muzzleflash and all the lights appear etc...
What I recommend is that you use Time.time to set your timer at the moment you fire the gun. That's when you should start emitting particles.
Then when (myTimer >= Time.time + $$anonymous$$uzzleFlashDuration), stop emitting particles.