- Home /
 
 
               Question by 
               GroundstompStudios · May 16, 2019 at 09:41 PM · 
                c#particlesunityeditorparticlesystemc# tutorial  
              
 
              Can someone tell me how to make a particle system play when hitting an animal?
Hi, I am currently creating a small survival game, and I want to know how I can make a particle system play when I hit an animal. I am new to programming, so if anyone could help me I would appreciate that.
Here is my script
     public float maxHealth, maxThirst, maxHunger;
     public float thirstIncreaseRate, hungerIncreaseRate;
     private float health, thirst, hunger;
     public bool dead;
 
     public GameObject player;
     public float[] position;
 
     public float damage;
     public bool weaponEquipped;
     public bool toolEquipped;
     public static bool triggeringWithAI;
     public static GameObject triggeringAI;
 
     public static bool triggeringWithTree;
     public static GameObject treeObject;
 
     public static bool triggeringWithOre;
     public static GameObject oreObject;
 
     [Header("Audio")]
 
     public AudioSource aud;
     public AudioSource swingAud;
     public AudioClip swing;
     public AudioClip impactAnimalAud;
     public AudioClip impactTreeAud;
     public AudioClip impactOreAud;
 
     [Header("UI Components")]
 
     public Text healthText;
     public Text thirstText;
     public Text foodText;
 
     public void Start()
     {
         health = maxHealth;
     }
 
     public void Update()
     {
         healthText.text = health.ToString();
         thirstText.text = thirst.ToString();
         foodText.text = hunger.ToString();
 
         if (!dead)
         {
             hunger += hungerIncreaseRate * Time.deltaTime;
             thirst += thirstIncreaseRate * Time.deltaTime;
         }
 
         if (thirst >= maxThirst)
             Die();
         if (hunger >= maxHunger)
             Die();
 
         //print(thirst);
         //print(hunger);
         //print(health);
 
         //detecting and killing ai
         if (triggeringWithAI == true && triggeringAI)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 Attack(triggeringAI);
             }
         }
 
         if (!triggeringAI)
             triggeringWithAI = false;
 
         //tree chopping
         if(triggeringWithTree == true && treeObject)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 Attack(treeObject);
             }
         }
 
         //ore mining
         if(triggeringWithOre == true && oreObject)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 Attack(oreObject);
             }
         }
     }
 
     public void Attack(GameObject target)
     {
         if (target.tag == "Animal" && weaponEquipped)
         {
             Animal animal = target.GetComponent<Animal>();
 
             aud.clip = impactAnimalAud;
             swingAud.clip = swing;
             swingAud.Play();
             aud.Play();
 
             animal.health -= damage;
         }
 
         if (target.tag == "Tree" && weaponEquipped)
         {
             TreeChopping tree = target.GetComponent<TreeChopping>();
 
             aud.clip = impactTreeAud;
             swingAud.clip = swing;
             swingAud.Play();
             aud.Play();
 
             tree.health -= damage;
         }
 
         if (target.tag == "Ore" && toolEquipped)
         {
             OreMining ore = target.GetComponent<OreMining>();
 
             aud.clip = impactOreAud;
             swingAud.clip = swing;
             swingAud.Play();
             aud.Play();
 
             ore.health -= damage;
         }
     }
 
     void PlaySound()
     {
         aud.PlayOneShot(swing);
     }
 
     public void Die()
     {
         dead = true;
         Destroy(player);
         print("You died.");
     }
 
     public void Drink(float decreaseRate)
     {
         thirst -= decreaseRate;
     }
 
     public void Eat(float decreaseRate)
     {
         hunger -= decreaseRate;
     }
 
     public void OnTriggerStay(Collider other)
     {
         if (other.tag == "Tree")
         {
             triggeringWithTree = true;
             treeObject = other.gameObject;
         }
 
         if (other.tag == "Ore")
         {
             triggeringWithOre = true;
             oreObject = other.gameObject;
         }
     }
 
     public void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Animal")
         {
             triggeringAI = other.gameObject;
             triggeringWithAI = true;
         }
     }
 
     public void OnTriggerExit(Collider other)
     {
         if (other.tag == "Animal")
         {
             triggeringAI = null;
             triggeringWithAI = false;
         }
     }
 
              
               Comment
              
 
               
              you can have an empty object on every weapon, position it to the place where you want the particles toplay.
 public void OnTriggerEnter(Collider other)
          {
              if (other.tag == "Animal")
              {
                  triggeringAI = other.gameObject;
                  triggeringWithAI = true;
                   particlesystem.transform.position = weaponHitPos;
                   particlesystem,Play();
              }
          }
 
 
                 Your answer