- Home /
Particles not appearing on impact, muzzle flash problems?
I'm having a problem with my muzzle flash and particles. I'm following the fps tutorial. I'm trying to use the Sparks prefab as my particles, and I set them as a child object of my gun. Also, I assigned muzzle_flash(the child of my gun) as my muzzle flash animation. However, when I shoot in the game I just get a white box as my muzzle flash. Also, my gun doesn't hit anything. I don't see sparks anywhere. I am using this shooting code for my machine gun:
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 : Renderer;
private var bulletsLeft : int = 0; private var nextFireTime = 0.0; private var m_LastFrameShot = -1;
function Start () { hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
function LateUpdate() { if (muzzleFlash) { // We shot this frame, enable the muzzle flash if (m_LastFrameShot == Time.frameCount) { muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward); muzzleFlash.enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
// We didn't, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () { if (bulletsLeft == 0) return;
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && bulletsLeft != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit;
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, hit, range)) {
// 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();
}
function Reload () {
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0) {
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () { return bulletsLeft; }
Answer by Michael 12 · Mar 22, 2011 at 12:28 AM
I had the exaxt same problem when I was following that tutorial so I feel your pain. Here follow this guys video tutorial, it helped me. He's a bit all over the place but gets the job done ;) http://www.youtube.com/watch?v=sYQQY76F8ac
Thats the tutorial I was following, still same problem.
Hmmm maybe copy and paste the script again, make sure everything is where it's supposed to be, if yur like me and new to this stuff it can be pretty easy to miss things. Even a simple spelling mistake can muck up the whole thing. Does Unity give you any error messages? If so check them for what line the error is on and then compare that to the code in the PDF document from the FPS tutorial.
Unity gives me the errors that my Windows is missing an update (SP1) and that there are two audio listeners in the scene. Would either of these cause a problem regarding muzzle flash and particles on impact?
Well I don't know about that first bit, but your machine gun should have an Audio Source NOT a listener so I would take a look in the inspector and make sure that is set right.
Answer by KnightChatX · Apr 17, 2011 at 07:55 AM
I'm experiencing the same problem, the sparks are showing up directly behind the camera no matter where I set it and will maintain a rotation behind the camera so that's why I can't see the sparks.
The other issue is the sparks are only visible in a top down view along the Y-Axis and only appear when firing directly in the sky or sometimes at the ground.
I reviewed the same tutorial, the muzzle flash and the gun you need to manually set the linkage to the associated textures.
I'm currently looking for a solution to the sparks problem, I'm using the default Sparks script that comes with Unity as well to test with.
Anybody have an answer how to fix this?