- Home /
How to use OnParticleTrigger correct?
I have a Gameobject and a ParticleEffect, the ParticleEffect has activated "Collision" and "Triggers". "Triggers" is set to kill on Enter when the GameObject enters. Thats all working fine the particles get killed. But how do I check for simple things like the tag of the Gameobject in script?
Answer by richardkettlewell · Apr 24, 2018 at 07:11 PM
You can see an example script here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleTrigger.html
$$anonymous$$aybe I am to stupid but I cant find there how to use "Trigger>>Enter>>$$anonymous$$ill"
Oh yes, apologies, $$anonymous$$ill is automatic and doesn't hook up to script.
To hook up to script, simply assign the Enter condition to callback ins$$anonymous$$d.
Now you can detect these events in script. To preserve the kill behavior, do it via script by setting the Particle.remainingLifetime property in the callback.
No worries, Thanks for your time now I understand :)
Answer by richardkettlewell · Apr 23, 2018 at 01:07 PM
Hi, unfortunately OnParticleTrigger doesn't provide any way to know which object the particle triggered. It's on our roadmap to add in the future.
If thats true for what is ParticleSystem>>Triggers>>Colliders else used? Because If I add a moving GameObject there and set "Enter" to kill, the Particles will only get killed by that GameObject and if you hover over ParticleSystem>>Triggers>>Colliders the Infobox-Tooltip says "The list of collision shapes to use for the trigger"
yes that is the list used. i simply mean that, when the callback is triggered, if you have 1 particle and 2 gameobjects, there is no way for you to find out which of the 2 gameobjects the particle triggered.
if you only have 1 gameobject in your list, then it's easy. just use ParticleSystem.trigger.GetCollider(0)
Thanks for your help Richard. I am not able to get that(ParticleSystem.trigger.GetCollider(0)) working correct, my debug.log is firing all the time and there is no GameObject to collide with, only particles from the ParticleSystem itself if thats possible. Would you please so kind and hand out a code snippet that prints a Debug.Log when Trigger>>Enter>>$$anonymous$$ill is activated?
Not really, but we have been discussing it again internally. A question for you: what would happen if more than one collider caused the trigger? Ie overlapping colliders. Should the callback be able to get both? Or is one enough?
Hey Richard, cool to hear that you're discussing it internally!
Both would be best, in case either object needs to react to the collision in a specific way.
For context, my use case for this is: I have a particle system that interacts with the world in a variety of collision behaviors. Its particles bounce off of world objects and they cause damage to the players and NPCs. I hope to add more interactions, such as a bomb going off that destroys particles.
The particle collision callbacks are limiting because OnParticleCollision(GameObject o) provides the game object but not the particles, and OnTriggerEnter/Inside() will provide the particles with GetTriggerParticles, but not provide the game object. I'm almost entirely dependent on the Particle System's built in Collision and Triggers module.
I'm not sure how the Unity collision system internals work, but I imagine both the colliding particle and the GameObject being collided with are available, and I'd imagine all the colliding objects are readily available to pass back to each callback function.
Answer by Lokym · Jun 10, 2019 at 04:18 PM
I know this post is a bit old, but I found a solution to detect a GameObject when the particle triggered something. You can Instantiate a collider at the position of the particle where it hit the gameObject, then use a OnTriggerEnter or Physics.OverlapBox / OverlapSphere or all that kind of things to do what you want.
// The collider to Instantiate. You can also use Resources.Load<GameObject>(). "
public GameObject colliderGO;
void OnParticleTrigger()
{
ParticleSystem ps = GetComponent<ParticleSystem>();
// particles
List<ParticleSystem.Particle> enter = new List<ParticleSystem.Particle>();
// get
int numEnter = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
// iterate
for (int i = 0; i < numEnter; i++)
{
ParticleSystem.Particle p = enter[i];
// instantiate the Game Object
Instantiate(colliderGO, p.position, Quaternion.identity);
enter[i] = p;
}
// set
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
}
NOTE: You have to set the SimulationSpace in the ParticleSystem to "World" to get the good p.position. Also, maybe the ParticleSystemTriggerEventType.Enter won't be accurate enough, and you will need to choose the ParticleSystemTriggerEventType.Inside, but be careful because it will instantiate a lot of gameObject.
And then, when your Game Object in instantiate, you do:
private void OnTriggerEnter(Collider other)
{
// what you want to do
// to avoid a stack of GameObjects
Destroy(gameObject);
}
Hope this still can help.
Really think this might be the solution i'm looking for but I'm struggling with the last bit. I'm pretty new to this so bare with!
"And then, when your Game Object in instantiate, you do:" this bit doesn't make sense.
Does that bit of code go on the Game Object you want to detect? So for example if the particle was trigger by an enemy I'd put the script on that enemy?
private void OnTriggerEnter(Collider other) { // what you want to do // to avoid a stack of GameObjects Destroy(gameObject);
Is the destroy command destroying the Collider? I presume that's the point? but that's currently named other. can't get y head around this ;/
Answer by shadowpuppet · Apr 23, 2018 at 12:50 PM
if you just want to know the name of the gameObject
void OnTriggerEnter(Collider collider) {
print (collider.gameObject.name);//or
print (collider.gameObject.tag);// I think
//if you want the particles to only get triggered on certain tags
void OnTriggerEnter(Collider other) {
if(other.CompareTag == ("Player")){
//DoSomethingHere
}
}
Ok Thanks, but I was talking about Particlesystem>>OnParticleTrigger and not about OnTriggerEnter!