- Home /
Help to trigger a rain event
Hi there,
I am simply wanting to tell the rain particle system to start when I reach a certain point and stop when I leave that area. I am new to Unity 3D so it's been hard to make sense of posts i've searched for on this topic.
So far I have rain (snow) and a box with mesh render turned off. I was told this is how to start off but not sure how to continue. ANY help greatly appreciated :)
Answer by Shawn Miller · May 09, 2012 at 11:38 PM
You actually don't need a mesh renderer at all. If you create a Particle System, you can add a Box Collider component to it, set the size of the area you want it to rain in, and make sure IsTrigger is checked on the Box Collider. Once you have that you can attach the following script (this is in C#) and as long as your player has the Player tag, when they walk through it, the particle effect will trigger.
public class ParticleTrigger : MonoBehaviour {
// Use this for initialization
void Start () {
particleSystem.Stop();
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Player") {
particleSystem.Play();
}
}
void OnTriggerExit(Collider other) {
if (other.tag == "Player") {
particleSystem.Stop ();
}
}
}
Hey $$anonymous$$, thanks again for the reply but I am only more confused now. How I tried to deal with this code doesn't seem to work. I am using version 3.4.0f5 of unity. I posted back the other day with an image but it doesn't seem to be here and this forum layout is very hard to work. Says you have posted 3 answers but I can only see 2??
Do I need to replace parts in this code with tags I have in the project?
$$anonymous$$atthew
This code is in C#- make sure you're not trying to put it in a UnityScript file. As for the layout, just remember that this isn't a forum- it's not really designed for long-winded, linear discussions. There's a question at the top, and a bunch of individual answers which should theoretically be sorted by an active and caring community to show you the most correct one. Unfortunately, it doesn't really seem to work like that, because there are only about 50 people who ever come here more than once and have the ability to vote on things.
Great thanks! So C# ae.. When I straight copy and past the above script into a C# script I get this error:
"Assets/Scripts/Rain_C.cs(1,32): error CS0246: The type or namespace name `$$anonymous$$onoBehaviour' could not be found. Are you missing a using directive or an assembly reference?"
Any ideas why? Do I attach this script to the particle system or the first person controller?
$$anonymous$$atthew
Well, it's because you're also overwriting the bit where it tells the compiler where to look for all that information! The lines
using System;
using UnityEngine;
are very important. Without that second one, it won't know what a $$anonymous$$onoBehaviour even is!
Add the script to the particle effect. $$anonymous$$ake sure the particle effect also has a box collider, set to trigger.
Thanks syclamoth! You're a REAL help. I'm now getting this error:
"Assets/Scripts/Rain_C.cs(8,8): error CS0103: The name particleSystem' does not exist in the current context" When I change
particleSystem' to the name tag for the particle system the same error pops up with the tag name ins$$anonymous$$d??
Your answer
Follow this Question
Related Questions
Trigger Event, Ring Pass thru Rod Object 3 Answers
create event on collision 2 Answers
How would I detect a right click with an event trigger? 3 Answers
Animation playing other object animation 0 Answers