- Home /
how to turn a particle emitter on & off with a rigidbody trigger?
Hi there,
I'm attempting to turn a waterfall on & off when the rock mesh is placed on top of it. When the rock is present, the emitter should be off, when the rock is not, the emitter should be on.
I have an invisible plane collider with the waterfall particle emitter attached as a child, I then have a rock-mesh with a rigidbody & collider attached to it, is trigger is also ticked on the rock.
I know I need to tag the rock, then tell the invisible plain "if the collision is true, turn off the emitter, if false turn on the emitter." but I'm not so sure how to go about doing this as i'm pretty new to coding.
If anybody could shed some light on this I would really appreciate this.
Thanks.
Answer by Lo0NuhtiK · May 21, 2012 at 02:54 AM
Something like this on the waterfall collider object (in C#):
public ParticleEmitter myEmitter ; //then drag/drop the emitter on here in inspector
/** or if you don't want drag/drop it -->**/ ParticleEmitter myEmitter ;
/** if myEmitter isn't public drag/drop -->
void Start(){
myEmitter = GetComponentInChildren<ParticleEmitter>() as ParticleEmitter ;
} <--**/
void OnTriggerEnter(Collider other){
if(other.CompareTag("Rock"))
myEmitter.emit = false ;
}
void OnTriggerExit(Collider other){
if(other.CompareTag("Rock"))
myEmitter.emit = true ;
}
Or if you want it on the rock... (in UnityScript):
var waterfall : ParticleEmitter ; //drag the waterfall emitter object on here
function OnTriggerEnter(other : Collider){
if(other.CompareTag("WatefallTriggerPlaneThing"))
waterfall.emit = false ;
}
function OnTriggerExit(other : Collider){
if(other.CompareTag("WaterfallTriggerPlaneThing"))
waterfall.emit = true ;
}
...somethin like that. All untested and I'm half-asleep again, but think I got it right (or at least close enough for you to get it figured out).
Hey man, i tried both methods and neither worked. I keep getting this error...
Assets/Waterfalltrigger.cs(8,6): error CS0116: A namespace can only contain types and namespace declarations
I kinda need the script attached to the waterfall plane so I havent tried your other method, what exactly am i doing wrong?
thanks
Ok I've fixed that problem, but my Rock (Trigger) keeps falling through the floor, This is because the Is Trigger is ticked, when i remove the tick it doesnt fall through the floor but obviously this removes the trigger. Any idea how I can get around this?
Thanks
Answer by Lboy · May 22, 2012 at 12:32 AM
Actualy I've fixed that but the problem now is the Rock (Trigger) keeps falling through the floor, this is because is Trigger is ticked, when i remove the tick it doesnt fall through the floor but this obviously removes the trigger needed to turn on the emitter.
Any idea how I can go about fixing this?
Thanks.