Hi. New to unity and scripting. Ive been learning “Trying” to use unity and java script for a few weeks now. Been watching and reading alot of tutorials on both. So i decided to create a little game of my own. I watched a tutorial on how to make a game called Lunar Landing from CG Cookies and used their code. They used the Legacy particle system in it, and i wanted to use the new Shuriken particle system instead. Wow, was that a big difference in coding. I looked everywhere and couldnt find a straight answer on how to code it into my game without errors. Googled it and tried all the suggestions people in the unity forums said. Nothing worked though. So i played with it and found the answer. Here is my Script.
Java Script not C# (for the new people)
#pragma strict
//variables Movement Dust
var forwardDust : ParticleSystem;
var backwardDust : ParticleSystem;
var leftDust : ParticleSystem;
var rightDust : ParticleSystem;
function Start ()
{
}
function Update ()
{
if (Input.GetAxis("Horizontal") > 0) //checking for right arrow key is down
{
rightDust.Stop(); //turns off dust
leftDust.Play(); //turns on dust
}
if (Input.GetAxis("Horizontal") < 0) //checking for left arrow key is down
{
leftDust.Stop(); //turns off dust
rightDust.Play(); //turns on dust
}
if (Input.GetAxis("Horizontal") == 0) //checking for no left or right keys is down
{
leftDust.Stop(); //turns off dust
rightDust.Stop(); //turns off dust
}
if (Input.GetAxis("Vertical") > 0) //checking for foward arrow key is down
{
backwardDust.Stop(); //turns off dust
forwardDust.Play(); //turns on dust
}
if (Input.GetAxis("Vertical") < 0) //checking for backward arrow key is down
{
forwardDust.Stop(); //turns off dust
backwardDust.Play(); //turns on dust
}
if (Input.GetAxis("Vertical") == 0) //checking for no foward or backward keys is down
{
forwardDust.Stop(); //turns off dust
backwardDust.Stop(); //turns off dust
}
}
A few things that i did to make it work::
-Make a Prefab of Particles used
-Place particles in a empty game object
-Empty game object parented to the source. ex “player”
-Turn on Prewarm in the particle edit options. Under Looping. (this was the small thing i was missing and made it work finally)
This is my first post and just wanted to post it just in case anyone else was having trouble with the same issue.
You probably don’t actually need the prewarm. The way you have it set up now, you are calling play/stop on every tick. If you call play/stop only on change, you will get a little more realistic simulation. The prewarm starts in a state of already emitting, stoping and starting without the prewarm, was probably causing each new emission to stamp out the previous.
Sorry to say, but yes. I do need the prewarm to make it work. Without it, the effects do not show at all.
But I apply the prewarm to the particles that are parented to my character and not to the prefab.
Could you show me an example with java script? I spent a few hours searching for a way to accomplish it and never came across the way that you described.
Well, yes, you need to use the prewarm because of how you have set up your script. But you are basically solving your problem with a hack/work-around. What is happening is that you have put everything in the Update without having any logic being performed.
What you want to do is to turn the particles on and off based on whether the key is pressed. But what are actually doing issuing a Play or Stop to every particle system every frame. So when you tried it first without the prewarm, it was actually working correctly, but you could’t see it because you were issuing the play command around 60 times a second. Basically preventing it from ever fully simulating. Prewarm starts the simulation with particles already in motion, that is why it appears to work.
The one on the left checks to see if it playing or stopped first, then only issues Play/Stop if required. Prewarm is not used.
The middle one is structured the way you have using the prewarm hack.
The right one is your original without the prewarm.
You can see it works just fine. It also emits more naturally as it is not starting in the middle. It would also be a bit more performance friendly, as it only plays or stops when actually needed not every frame.
Thank you zombiegorilla for putting together the example and taking time out your day to do this.
I kinda understand what you mean, but have no idea how to put it together in code. Is it like how chararterController works? Were you have to make a private var for it and add “charController = GetComponent(CharacterController);” to the function start to make it accessible in the function update, instead of manualy dropping it in??
a code example would be greatly appreciated, as Im a visual learner.
Thank you again.
PS I’ve been doing this for only 3 weeks now in my spare time and have no prior experience in coding or game design what so ever.
The best thing to do is to add an additional function for a specific particle with a list of sub commands e.g. particle duration, then you can start adding co-routines to use procedural updates instead of using it on a per frame basis as I’m not sure exactly what you are trying to do, this method will allow you to modify it until your heart’s content:
That a very simple way to do it (and I only used one particle system). Alternately you could actually check the particle system, which is good if you have other scripts that may affect it as well:
That way is a tiny bit more expensive as it is checking with an external object.
You could also add the scripts directly to the particle systems, and let them perform the key checks for themselves.
I had done something similar a while back for space ship thrusters, they had a little more complexity as they had animations and lights in addition to the particles. In that case I gave them public getters and setters that let me set a value on them and they did all the rest internally. Ultimately there are many approaches to doing just about everything, that is the fun of development. The trick is just finding the optimal one for the particular needs of the task at hand.