- Home /
How to Make Particle Effects Function as a Trigger That Deals Damage
In my game, I have numerous particle effects that I want to damage the player if they touch it (fire, water, gas.) I started using the built in collision system, and I got the damage working, but I could only get it to function with non-trigger colliders. So the water does damage the player, but it also bounces off like little blue balls. I've tried using the trigger tab on the particle system and swapping in the code equivalent, but I do not understand it fully and it will not work. Here is the code I am using. For clarification this is on the player script and the object containing the particle system is tagged as "Muse Attack".
void OnParticleCollision(GameObject other)
{
Debug.Log("Hit");
if(other.tag == "Muse Attack")
{
TakeDamage(1);
}
}
As well as how I set the particle system up. A lot of sources tell me to just line up a collider with the effect, but with the water especially it follows a physics based arc, so that won't work. Particle collision is just confusing me in general so any help is appreciated.
Answer by Envans · Nov 26, 2020 at 10:32 AM
Your questions talk about triggers yet you have collision ticked use the triggers module of the particle system define which colliders it can "collide" with then handle the rest in a script using the callback option of the dropdowns. You can read further here https://docs.unity3d.com/2019.3/Documentation/Manual/PartSysTriggersModule.html
also you are using particle collision wrong, read further here https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnParticleCollision.html
As I wrote in the question I tried the Trigger module and it didn't work. But I did give it another shot. What the big difference seems to be is the OnParticleTrigger code needs to be in a script attached to the Particle System itself. I did that and it does work sort of, except the particles damage my player whenever they touch anything. How do I make it differentiate between which collider it's touching?
public class ParticleDamageScript : $$anonymous$$onoBehaviour
{
public ParticleSystem ps;
public $$anonymous$$agpieScript magpie;
// Start is called before the first frame update
void Start()
{
ps = GetComponent<ParticleSystem>();
magpie = FindObjectOfType<$$anonymous$$agpieScript>();
}
// Update is called once per frame
void Update()
{
}
public void OnParticleTrigger()
{
magpie.TakeDamage(1);
}
*Colymbas and magpie refer to the player.
You are better off studying the docs as I am not versed enough in particle collision to help you and I don't even know if trigger is better for your case than collider. In any case in the Unity docs it says that you need to get each particle that trigger the Enter or Inside event in you case then you can check the details for each particle, I am not 100% sure that triggers can work for 2d colliders. Also in this https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnParticleCollision.html?_ga=2.57290986.1593235625.1605796702-1106866686.1596461765 you can see that you can get collision event data for each particle and most likely you will be able to check the tag of the collision or the layer. I wish you the best of luck with your project.!