- Home /
Muzzle Flash Works In Editor - But Not Build??
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.
Can you hear the audio being played in the build? When do you set m_LastFrameShot?
Updated script. And Yeah - I can still hear my audio.
Answer by Eric5h5 · Apr 28, 2011 at 12:19 PM
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.
Any number of ways, yield WaitForSeconds probably being the simplest.
Answer by tomekkie · Apr 25, 2011 at 02:26 PM
Do not use editor classes like Debug. They are only for using in the Unity editor and are not available at runtime.
As far as I know Debug is an Engine class so this should not be the problem.
@tomekkie: That's not actually correct; Debug.Log works fine in a build, and Debug.isDebugBuild is intended to be used in a build (it just returns true in the editor). DrawLine/DrawRay don't though.
if you check the debug build checkbox when building then isdebug will return true in your build too.