- Home /
.Js Particle issue (probably easy for moderate coders)
Hi all, i am working on a gun, the barrel of which emits particles in place of bullets merely as a visual element, they do no damage.
all that is working ok but it continues to emit while the gun's reload sequence happens and the fire button is down, i need the emitter to stop producing whether the fire key is down or not during the reload, and then continue emitting only on the key press after.
to make thins worse the Emit_on_Keypress script is in one file and the Reload_Func. of the gun is part of another.
all i need is something to kill the emitter while the gun reloads and then let it go again when the reload is done (3.3 sec) while referencing commands between the two scripts.
i imagine this is easy if you know what you are doing but if you haven't guessed by now i am a total noob to coding, and have hit a wall.
-------------this is the first script that controls the gun--------------------------------
var Range : float = 1000;
var Force : float = 1000;
var Clips : int = 20;
var BulletPerClip : int = 60;
var ReloadTime : float = 3.3;
var Damage : int = 9;//this is the value of damage per hit
var BulletsLeft : int = 0;
var ShootTimer : float = 0;
var ShootCooler : float = 0.9;
public var ShootAudio : AudioClip;
public var ReloadAudio : AudioClip;
var HitParticles : ParticleEmitter;
var MuzzleFlash : ParticleEmitter;
var MuzzleCooler : float = 0.9;
var MuzzleFlashTimer : float = 0;
var Light1 : GameObject;
var Light2 : GameObject;
var Light3 : GameObject;
var MuzzleSpeed : int = 200000;
var KeyTimer : float = 0;
var KeyCooler : float = 1;
function Start (){ BulletsLeft = BulletPerClip; MuzzleFlash.emit = false; HitParticles.emit = false; Light1.active = false; Light2.active = false; Light3.active = false; }
function Update () { if( KeyTimer > 0){
KeyTimer -= Time.deltaTime;
}
if( KeyTimer < 0){
KeyTimer = 0;
}
if ( MuzzleFlashTimer > 0){
MuzzleFlashTimer -= Time.deltaTime;
MuzzleFlashShow();
}
if( MuzzleFlashTimer < 0){ MuzzleFlashTimer = 0;
}
if( ShootTimer > 0){
ShootTimer -= Time.deltaTime;
}
if( ShootTimer < 0){
ShootTimer =0;
}
if(KeyTimer == 0){ if(Input.GetMouseButton(0) && BulletsLeft){//GetKey(KeyCode.F) = rapid fire, GetKeyDown(KeyCode.F) = single fire, Replace with GetMouseButton(0)when ready 0=L, 1=R, 2=M if( ShootTimer == 0){
PlayShootAudio(); RayShoot();
ShootTimer = ShootCooler; }
if ( MuzzleFlashTimer ==0){
MuzzleFlashTimer = MuzzleCooler; MuzzleFlashShow();
} } } }
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.blue); 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);
}
------------------------------And the Emit on KeyPress-----------------------------
var emissionTime : float = 3.0;
var emissionDelay : float = 3.0;
var lastTime = 0.0;
function Update() { if (Input.GetMouseButton(0)){ GetComponent(ParticleAnimator).autodestruct = false; particleEmitter.emit = true; lastTime = Time.time; } if (particleEmitter.emit && (Time.time - lastTime) > emissionTime) { particleEmitter.emit = false; }
}
there is probably an easier way to get the desired result, i would take that too, this is all just stuff i stitched together watching tuts online.
Any help would be appreciated, thanks!.
Your answer
Follow this Question
Related Questions
How do I make a gun project a particle? 1 Answer
Fps game gun reloading problem 1 Answer
Particles not appearing on impact, muzzle flash problems? 2 Answers
How to position a gun on a FPS Charater 1 Answer
FPS gun clippes through walls 5 Answers