- Home /
Particle System emits on Trigger Stay, in Unity 5
Hi, I'm trying to have a particle system emit only while a tagged object is touching/overlapping it. I need code for a trigger event that works with Unity 5 and specifies the tag of the object that touches it ("rightSide").
So far I know I need onTriggerStay (most code examples are one hit onTriggerEnter events), and one of the two objects need to have a Rigidbody component. http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerStay.html
The Unity docs say to use this true/false format instead of .Play and .Stop: ParticleEmitter.enabled public bool enabled; http://docs.unity3d.com/ScriptReference/ParticleEmitter-enabled.html
I found a great video explaining trigger vs collider and that we should never use Collide unless there's two physics objects coming together where it matters what angle and exact polygon(s), like in emitting dust at the right angle after a ball hits the ground. It also mentions concave shapes won't work as colliders. https://www.youtube.com/watch?v=IG2Bj80IT2Q
Old Code just for reference. Uses key press to toggle and works for other purposes.
using UnityEngine; using System.Collections;
public class light : MonoBehaviour { protected bool letPlay = true;
public void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
letPlay = !letPlay;
}
if (letPlay)
{
if (!gameObject.GetComponent<ParticleSystem>().isPlaying)
{
gameObject.GetComponent<ParticleSystem>().Play();
}
}
else
{
if (gameObject.GetComponent<ParticleSystem>().isPlaying)
{
gameObject.GetComponent<ParticleSystem>().Stop();
}
}
}
}
Answer by NightLucididty · Nov 07, 2015 at 10:41 AM
I do not think that you can specify on what side of the collider the collision is but you can create an empty game object with a collider and attach it as a child to your main object, and have the collider be on the right side. And than you could do the same for another side if you have a similar function for another side like left, up, down... As for the code, now something similar to what was on the Scripting API,
void OnTriggerStay(Collider collider) {
if (collider.tag == "TagGivenToChildGameObject")
gameObject.GetComponent<ParticleSystem>().Play();
}
You say that you need to enable it or disable it as a bool, but that is only another method to do it, Your old code should work, Feel free to reply if you have anything else, Nightlucidity
Thanks! Adding a child Collider object on the side worked best like you said. I'd run into another issue and wasn't sure if it was my code's fault until I resolved it. Works great now, sorry for the slow response!
void OnTriggerEnter(Collider other) { if (other.tag == "wand") { stars.Play (); Debug.Log ("object Wand has started the particle emitter Stars"); } else { Debug.Log ("something other than Wand has entered triggerspace"); return; }
}
void OnTriggerExit(Collider other)
{
if (other.tag == "wand")
{
stars.Stop();
Debug.Log("object Wand has stopped the particle emitter Stars");
} else {
Debug.Log ("something other than Wand has exited triggerspace");
return;
}
}
}
Your answer
Follow this Question
Related Questions
Have "OnTriggerStay" detection but only call it once? 1 Answer
Plugin to integrate .pex file compatible with unity 3D 0 Answers
BigExplosion won't stop bursting 0 Answers
Making a texture influence particle lifetime 0 Answers
Simple one-way teleport? 2 Answers