How can I make different particle system to play when hitting different game objects?

I’m currently working at a small fps. I made a small script for a weapon that user raycasts to shoot. When those raycasts hit a certain point they create some impact particles. What i want is when i hit an enemy, I need to play some different particles (for ex. blood) but when i hit the wall play other particles.
Here’s my gun script:

void Start()
{
    anim = GetComponent<Animation>();
    anim.Play("recoilrifle");
    audioData = GetComponent<AudioSource>();
    Overheat = 0f;
    ammo = 40f;
}
// Update is called once per frame
void Update()
{
    ammoCount.text = ammo.ToString("00");
    if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
    {
        nextTimeToFire = Time.time + 1f / fireRate;
        Shoot();
    }
    
}



public void OverheatWeapon()
{
    heat.StopGun(); 
}

public void ResetFloat()
{
     Overheat = 0f;
    ammo = 40f;
}

void FixedUpdate()
{
    if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    {
        anim.Play();
        anim.Play();
    }
}

void Shoot()
{
    muzzleflash.Play();
    Overheat += 1f;
    ammo -= 1f;
    anim.Play("recoilrifle");
    audioData.Play();
    RaycastHit hit;
    if (Physics.Raycast(fpscamera.transform.position, fpscamera.transform.forward, out hit, range))
    {
        UnityEngine.Debug.Log(hit.transform.name);

        Target target = hit.transform.GetComponent<Target>();
        if (target != null)
        {
            target.TakeDamage(damage);
        }

        if (hit.rigidbody != null)
        {
            hit.rigidbody.AddForce(-hit.normal * impactForce);
        }

        GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
        Destroy(impactGO, 2f);

    }
    if (Overheat > 40f)
    {
        OverheatWeapon();
    }
    
}

}

Also my target script:

void Start()
{
    animator = GetComponentInChildren<Animator>();
    animator.SetBool("Die", false);
    Active = true;
    animator = GetComponentInChildren<Animator>();

}

public void TakeDamage(float amount)
{
    health -= amount;
    animator.SetBool("Hit", true);
    Invoke("HitFalse", delay2);
    psh.Play();
    

    if (health <= 0f)
    {
        
        animator.SetBool("Aware", false);
        animator.SetBool("Atack", false);
        ai.GetComponent<EnemyAiTutorial>().enabled = false;
        
        animator.SetBool("Die", true);
        ps.Play();
        Invoke("Follow", delay1);
        Invoke("Die", delay);
        Invoke("ActivePortal", delay1);
    }
}
private void HitFalse()
{
    animator.SetBool("Hit", false);
}
void ActivePortal()
{
    activatePortal.Increase();
}
void Follow()
{
    Active = false;
}
void Die()
{
    Destroy(gameObject);
}
private void Update()
{
    if (health <= 0f)
    {
        animator.SetBool("Hit", false);
    }
}

Thanks in advance!

Prerequisite :
1.For this example you need 2 tags, “Enemy” and “Static” (You can create more if you want to have more variety of particle effects)
2.Assign “Enemy” to all the enemies in the scene, and “Static” to all the walls, trees, etc…
3. A particle effect for both the enemies and static objects (which you should already have)


Code :

 public GameObject StaticParticle; //Particle for static objects, reference in inspector
 public GameObject EnemyParticle; //Particle for enemy objects, reference in inspector

 void Start()
 {
     anim = GetComponent<Animation>();
     anim.Play("recoilrifle");
     audioData = GetComponent<AudioSource>();
     Overheat = 0f;
     ammo = 40f;
 }
 // Update is called once per frame
 void Update()
 {
     ammoCount.text = ammo.ToString("00");
     if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
     {
         nextTimeToFire = Time.time + 1f / fireRate;
         Shoot();
     }
     
 }
   
 public void OverheatWeapon()
 {
     heat.StopGun(); 
 }
 
 public void ResetFloat()
 {
      Overheat = 0f;
     ammo = 40f;
 }
 void FixedUpdate()
 {
     if (Input.GetAxis("Mouse ScrollWheel") > 0f)
     {
         anim.Play();
         anim.Play();
     }
 }
 void Shoot()
 {
     muzzleflash.Play();
     Overheat += 1f;
     ammo -= 1f;
     anim.Play("recoilrifle");
     audioData.Play();
     RaycastHit hit;
     if (Physics.Raycast(fpscamera.transform.position, fpscamera.transform.forward, out hit, range))
     {
         UnityEngine.Debug.Log(hit.transform.name);
         Target target = hit.transform.GetComponent<Target>();
         if (target != null)
         {
             target.TakeDamage(damage);
         }
         if (hit.rigidbody != null)
         {
             hit.rigidbody.AddForce(-hit.normal * impactForce);
         }

         if (hit.transform.tag == "Enemy")
         {
             GameObject impactGO = Instantiate(EnemyParticle, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(impactGO, 2f);
          }
          else if (hit.transform.tag == "Static")
          {
             GameObject impactGO = Instantiate(StaticParticle, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(impactGO, 2f);
          }
     }
     if (Overheat > 40f)
     {
         OverheatWeapon();
     }   
 }

I don’t think you need to make any changes to the Target script :slight_smile: