Raycast & Emitter issues!

Hey guys! Since you guys were of such great help before I thought I’d ask for some more help! Anyway, I have a Raycast in my game for my player’s machine gun. Everything went fine, but my issue, however, are the Particle Emitters. When the Fire button is held down, the muzzleflash should go off; however, it simply doesn’t. Everything in the public variables are in place (like the emitters), and they work in the editor. Just not in conjunction with the script.

Here’s the first block:

    void Start ()
    {

        WallSparks.emit = false;
        MuzzleFlashEMITTER.emit = false;

    }
   
    // Update is called once per frame
    void Update ()
    {
        if(Input.GetButton("Fire1"))
        {
            MuzzleFlashEMITTER.Emit();

            RayShoot();
        }
    }

The same goes for the sparks that are supposed to fly out when it makes contact with a rigidbody. As shown below:

    void RayShoot ()
    {
        RaycastHit hit;
        Vector3 DirectionRay = transform.TransformDirection (Vector3.right);
        Debug.DrawRay (transform.position, DirectionRay * Range, Color.yellow);

        if(Physics.Raycast(transform.position, DirectionRay, out hit, Range))
        {
            if(hit.rigidbody)
            {
                if(WallSparks)
                {
                    WallSparks.Emit();
                    WallSparks.transform.position = hit.point;
                    WallSparks.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
                }

                hit.collider.SendMessageUpwards ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
            }
        }
    }

As with my previous post, I will post the entire source file below. It’s just really strange. I’ve been able to re-purpose the Emitter script for other stuff but not this. Must be something obvious I just missed! Anyway, Please & Thank-You guys!

1894495–121978–RayShot.cs (1.1 KB)

Bump!

I use that same script in one of my projects.
I had to take out his muzzleflash stuff and put my own in that uses a particle system instead of the legacy particles because like you I could not get it to work at all.
Yet I never had trouble with the hit particles.

void RayShooter()
    {
        RaycastHit hit;
        Vector3 directionRay = transform.TransformDirection(Vector3.forward);
        Debug.DrawRay (transform.position, directionRay * range, Color.green);
     
        if (Physics.Raycast(transform.position, directionRay, out hit, range))
        {
            if (hit.rigidbody)
            {
                if (hitParticle)
                {

                    hitParticle.transform.localPosition = hit.point;
                    hitParticle.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
                    hitParticle.Emit();
                 
                    hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);
                    hit.collider.SendMessageUpwards("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
                }
            }
        }

If you compare my code to yours you can see your mistakes with the hit particles.
Ignore where I have TakeDamage instead of ApplyDamage because it is only because I use a different script for my AI than the one he has you write.

As far as the Muzzle Flash this is what I did

first get rid of everything he had you put in there for the muzzleflash except the

void Muzzleflash
{

}

add this variable at the top of your class some where

public ParticleSystem muzzleFlash;

My fire statement looks like this now.

if (Input.GetButton("Fire1") && bulletsLeft > 0 && clips > 0)
        {
            if (shotTimer == 0)
            {
                PlayShotAudio();
                RayShooter();
                shotTimer = shotCooler;
                keyTimer = keyCooler;
                    MuzzleFlash();
            }

        }

And down in

void MuzzleFlash()
{

}

make it look like this

void MuzzleFlash()
    {
        muzzleFlash.Play();
    }

Make a muzzleflash particle system with an empty gameobject and add component particle system.
Then adjust it the way you want it to look and it should work fine.

I hope i made some sense or helped you out even a little.

If you need I can just give you my entire script if this all confuses you too much.