Hello,
I have a turret that has a muzzle flash that is on for one frame. In the editor, you can clearly see that the muzzle flash when it fires, whereas in the build, you only see it sometimes. I'm not sure if its to do with the LateUpdate? or FrameCounting? I'm not really familiar with all that. Below is the code that controls the muzzle flash:
function LateUpdate() {
var ExternalResearch : TurretResearch;
ExternalResearch = gameObject.GetComponent("TurretResearch");
if (ExternalResearch.CurrentSelectedFlash) { //muzzleFlash
if (m_LastFrameShot == Time.frameCount) {
audio.clip = TurretSound;
audio.Play();
ExternalResearch.CurrentSelectedFlash.gameObject.SetActiveRecursively(true);
} else {
ExternalResearch.CurrentSelectedFlash.gameObject.SetActiveRecursively(false);
}
}
}
function FireOneShot () {
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
var localOffset = transform.position + transform.up * 2;
var layerMask = 1 << 10;
layerMask = ~layerMask;
// Did we hit anything?
//transform.position + transform.up * 10
if (Physics.Raycast (localOffset, direction, hit, 90000, layerMask) && MachineGun == true) {
Debug.DrawLine (localOffset, hit.point, Color.yellow);
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0)
Reload();
}
Any reason why its doing that? I would appreciate any help.
Thanks folks.
You should probably make the flash last for a specified period of time (1/20th of a second or something), rather than one frame. The game runs faster in a build than the editor, and if you're getting, say, 150 fps and your screen refresh is 60Hz, then it's somewhat likely a single-frame flash wouldn't even be drawn by the monitor.
Do not use editor classes like Debug. They are only for using in the Unity editor and are not available at runtime.