- Home /
Question by
EuroFight · Apr 09, 2012 at 12:16 AM ·
javascriptfpslightlens-flare
Lens-Flare Muzzleflash
Hi everyone
I am using a lens-flare attached to a light as a muzzle flash in an FPS game using the JavaScript code below (Modified from the FPS tutorial).
However, when I run the game, the light appears to work, however the lens flare is not visible. The 'muzzleFlash' light object is a point light, with a flare enabled.
Does anybody have any idea what I am doing wrong?
Thanks in advance
var range = 100.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 40;
var clips = 20;
var reloadTime = 0.5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Light;
private var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;
function Start () {
hitParticles = GetComponentInChildren(ParticleEmitter);
if (hitParticles)
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
function LateUpdate() {
if (muzzleFlash) {
if (m_LastFrameShot == Time.frameCount) {
muzzleFlash.enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
muzzleFlash.enabled = false;
enabled = false;
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () {
if (bulletsLeft == 0)
return;
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
while( nextFireTime < Time.time && bulletsLeft != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () {
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if (Physics.Raycast (transform.position, direction, hit, range)) {
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
m_LastFrameShot = Time.frameCount;
enabled = true;
if (bulletsLeft == 0)
Reload();
}
function Reload () {
yield WaitForSeconds(reloadTime);
if (clips > 0) {
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () {
return bulletsLeft;
}
Comment