- Home /
How To Make Particles Play On Impact?
Hello. I was wondering how would I make particles show when I hit an animal, tree, and ore. I am new to unity and want to know how to do this. If you guys could help me I would appreciate that.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public float maxHealth, maxThirst, maxHunger;
public float thirstIncreaseRate, hungerIncreaseRate;
private float health, thirst, hunger;
public bool dead;
public GameObject player;
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("Particles")]
public ParticleSystem animalImpact;
public ParticleSystem treeImpact;
public ParticleSystem oreImpact;
[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);
}
}
// *****CHANGE IF IT DOESN'T WORK*****
public void Attack(GameObject target)
{
if (target.tag == "Animal" && weaponEquipped)
{
Animal animal = target.GetComponent<Animal>();
animal.health -= damage;
}
if (target.tag == "Tree" && weaponEquipped)
{
TreeChopping tree = target.GetComponent<TreeChopping>();
tree.health -= damage;
}
if (target.tag == "Ore" && toolEquipped)
{
OreMining ore = target.GetComponent<OreMining>();
ore.health -= damage;
}
}
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;
}
}
}
Answer by Pangamini · May 14, 2019 at 09:25 PM
You could create an instance of your particle system (or even more complex gameObject with other effects, light, sound, etc) on impact. If you know the position of the hit, you can for example do
Object.Instantiate(animalImpact, hitPosition, Quaternion.LookRotation(hitNormal), target.transform)
this will create a copy of your particle system, and parent it to the target (so it moves together with it). Set your particle system to play automatically and to destroy itself once it's done. Or instantiate a gameObject with some kind of MaxLifetime component, that will destroy the gameObject after some time. More advanced technique to optimize garbage allocation would be to have a pool of objects, and repurpose the effects that have finished playing, instead of destroying them.
What would the target.transform and the hitNormal be? I am assu$$anonymous$$g the hitPosition is a Transform?
Well, if you look at the documentation of the given method HERE, you'd see that the position is a Vector3 world position where you want the new object to be located, so the hit position. The NOR$$anonymous$$AL is a vector perpendicular to the hit surface, so you can rotate it right, as co$$anonymous$$g out of the surface. If you do a raycast or a collision, you usually receive a normal. Target.transform is, well, the transform of a target. It's meant to attach the newly created object as a child of the target, so they move together
Your answer
Follow this Question
Related Questions
Can someone tell me how to make a particle system play when hitting an animal? 0 Answers
Can you change the Velocity Over Lifetime of a Particle System using a Script? 3 Answers
Assign random start lifetime for particle system from script 2 Answers
Turning my grid of particles into a sphere 0 Answers
How to make an effect in a particle system so that it is always behind the sphere? 1 Answer