- Home /
Particle system activate onTriggerEnter2D
I've tried converting this:
public class ParticleTrigger : MonoBehaviour { [SerializeField] ParticleSystem speedBoostParticle = null;
public void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
speedBoostParticle.Play();
}
}
} Into this
public class ParticleTrigger : MonoBehaviour { [SerializeField] ParticleSystem speedBoostParticle = null;
public void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
speedBoostParticle.Play();
}
}
}
But for some reason the OnTriggerEnter thing does not work. Also a note the object which this script is on is destroyed 0.1 seconds after collision with player as it is a powerup pickup
Answer by varigeri92 · Aug 22, 2020 at 11:10 PM
Do you have a rigidbody on your player?
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.
Why it Destroys its should come from an another script or if the particle system is on the same GameObject as this script. just use a different Stop Action on the particle system. you find the options in a dropdown in the particle settings its propably set to Destroy... try none or Callback and destroy or reuse If you need.
$$anonymous$$y player does have a Rigidbody yes, the player controller script is responsible for destroying the powerup gameobject. I've tried hosting the particle trigger script on both the player gameobject, as well as on the powerup itself. Only the powerup uses the "IsTrigger" whilst the play does not. The particle system is not set to play on awake and is therefor set to play just once before it gets destroyed along with its host object - the particle system is not a child of the powerup object.
Ok, i found the solution, instead of just playing an active particle system in the scene, i had to instantiate a particle system, and then play that one
Answer by ZozeR · Aug 23, 2020 at 06:08 PM
Either one of the objects has to have Trigger collider and also a rigid body.
//Declare the particle effect game object and put it in the editor.
public GameObject effect_par;
//Inside the ontrigger
private void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Player")
Instantiate(effect_par, transform.position, transform.rotation);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613010303im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ParticleSystem.emission randomly wont reset 1 Answer
Particles don't always play 2 Answers
How do I change Particle System color via script in Unity 5.6.1f1? 1 Answer
Flip Particle System Graph 0 Answers