- Home /
How can i group together ParticleSystems in JS
Hello,
Here is my SpaceShip controller, Its a complete mess at the moment as i am fairly new to scripting but i do have basic understanding, Yet i cant seem to work this out even after hours of googling.
Q. Is there a better way to group together these engines / thrusters into there own category then call them, For example Engines "Engines.enableEmission = true;", Left Thrusters "LeftThrusters.enableEmission = true;" , Right Thrusters "and so on", Rather than the wall of "var"s i have, The way i have it is also problematic as if i have a smaller ship with less engines, i cant fill each particle and get errors, Thanks.
//Engines
var LLEngine : ParticleSystem;
var LEngine : ParticleSystem;
var REngine : ParticleSystem;
var RREngine : ParticleSystem;
//Front Thrusters
var FRBThruster : ParticleSystem;
var FRMThruster : ParticleSystem;
var FRFThruster : ParticleSystem;
var FLFThruster : ParticleSystem;
var FLMThruster : ParticleSystem;
var FLBThruster : ParticleSystem;
//Top Thrusters
var TRBThruster : ParticleSystem;
var TRFThruster : ParticleSystem;
var TLBThruster : ParticleSystem;
var TLFThruster : ParticleSystem;
//Bottom Thrusters
var BFRThruster : ParticleSystem;
var BFLThruster : ParticleSystem;
var BBLThruster : ParticleSystem;
var BBRThruster : ParticleSystem;
//Left Thrusters
var LFBThruster : ParticleSystem;
var LFTThruster : ParticleSystem;
var LBBThruster : ParticleSystem;
var LBTThruster : ParticleSystem;
//Right Thruster
var RBTThruster : ParticleSystem;
var RBBThruster : ParticleSystem;
var RFTThruster : ParticleSystem;
var RFBThruster : ParticleSystem;
var ForwardSpeed = 1000;
var ReverseSpeed = 500;
var StrafeSpeed = 250;
var TurnSpeed = 25;
function Start () {
}
function Update () {
//Go Forward
if(Input.GetKey(KeyCode.W)){constantForce.relativeForce.z = ForwardSpeed * Time.deltaTime;
LLEngine.startSpeed = 1;
LEngine.startSpeed = 0.5;
REngine.startSpeed = 0.5;
RREngine.startSpeed = 1;
}else{
LLEngine.startSpeed = 0.2;
LEngine.startSpeed = 0.05;
REngine.startSpeed = 0.05;
RREngine.startSpeed = 0.2;
//RREngine.enableEmission = false;
//Go Back
if(Input.GetKey(KeyCode.S)){constantForce.relativeForce.z = -ReverseSpeed * Time.deltaTime;
FRBThruster.enableEmission = true;
FRMThruster.enableEmission = true;
FRFThruster.enableEmission = true;
FLFThruster.enableEmission = true;
FLMThruster.enableEmission = true;
FLBThruster.enableEmission = true;
}else{
FRBThruster.enableEmission = false;
FRMThruster.enableEmission = false;
FRFThruster.enableEmission = false;
FLFThruster.enableEmission = false;
FLMThruster.enableEmission = false;
FLBThruster.enableEmission = false;
constantForce.relativeForce.z = 0;}}
//Strafe Left & Right
if(Input.GetKey(KeyCode.E)){constantForce.relativeForce.x = StrafeSpeed * Time.deltaTime;
LFBThruster.enableEmission = true;
LFTThruster.enableEmission = true;
LBBThruster.enableEmission = true;
LBTThruster.enableEmission = true;
}else{
LFBThruster.enableEmission = false;
LFTThruster.enableEmission = false;
LBBThruster.enableEmission = false;
LBTThruster.enableEmission = false;
if(Input.GetKey(KeyCode.Q)){constantForce.relativeForce.x = -StrafeSpeed * Time.deltaTime;
RBTThruster.enableEmission = true;
RBBThruster.enableEmission = true;
RFTThruster.enableEmission = true;
RFBThruster.enableEmission = true;
}else{
RBTThruster.enableEmission = false;
RBBThruster.enableEmission = false;
RFTThruster.enableEmission = false;
RFBThruster.enableEmission = false;
constantForce.relativeForce.x = 0;}}
//Rotate Up and Down
if(Input.GetKey(KeyCode.D)){constantForce.relativeTorque.y = TurnSpeed * Time.deltaTime;
LFBThruster.enableEmission = true;
LFTThruster.enableEmission = true;
RBTThruster.enableEmission = true;
RBBThruster.enableEmission = true;
}else{
LFBThruster.enableEmission = false;
LFTThruster.enableEmission = false;
RBTThruster.enableEmission = false;
RBBThruster.enableEmission = false;
if(Input.GetKey(KeyCode.A)){constantForce.relativeTorque.y = -TurnSpeed * Time.deltaTime;
LBBThruster.enableEmission = true;
LBTThruster.enableEmission = true;
RFTThruster.enableEmission = true;
RFBThruster.enableEmission = true;
}else{
LBBThruster.enableEmission = false;
LBTThruster.enableEmission = false;
RFTThruster.enableEmission = false;
RFBThruster.enableEmission = false;
constantForce.relativeTorque.y = 0;}}
//Rotate right and left
if(Input.GetKey(KeyCode.Space)){constantForce.relativeForce.y = StrafeSpeed * Time.deltaTime;
BFRThruster.enableEmission = true;
BFLThruster.enableEmission = true;
BBLThruster.enableEmission = true;
BBRThruster.enableEmission = true;
}else{
BFRThruster.enableEmission = false;
BFLThruster.enableEmission = false;
BBLThruster.enableEmission = false;
BBRThruster.enableEmission = false;
if(Input.GetKey(KeyCode.LeftControl)){constantForce.relativeForce.y = -StrafeSpeed * Time.deltaTime;
TRBThruster.enableEmission = true;
TRFThruster.enableEmission = true;
TLBThruster.enableEmission = true;
TLFThruster.enableEmission = true;
}else{
TRBThruster.enableEmission = false;
TRFThruster.enableEmission = false;
TLBThruster.enableEmission = false;
TLFThruster.enableEmission = false;
constantForce.relativeForce.y = 0;}}
}
Answer by drod7425 · Nov 06, 2014 at 06:05 PM
Haha wow, that is a bit of a mess :)
There are a few approaches you could take with this, but I would make a Thruster class that contains a Particle System and an enumeration of ThrusterLocations (TOP,BOTTOM,FRONT,BACK,LEFT,RIGHT). Something like this (Please excuse me. I'm more comfortable with C#, so I may make some mistakes):
enum ThrusterLocation{
TOP,BOTTOM,FRONT,BACK,LEFT,RIGHT
}
class Thruster extends Monobehaviour{
var particleSystem : ParticleSystem;
var thrusterLocation : ThrusterLocation[3]; //each of your thrusters had three axes
}
Then I would put that script on each thruster in the scene and assign the appropriate variables in the inspector. Finally, in the script you showed, I'd make an Thruster[] that I could set in the inspector. Like this:
var thrusters : Thruster[22];
function Update(){
if(Input.GetKey(KeyCode.S)){
for(var thruster in thrusters){
thruster.particleSystem.enableEmission = thruster.thrusterLocation[0]==ThrusterLocation.FRONT;
}
}
//And so on for all the Input keys and their appropriate thrusters
}
That's just the one way that my mind immediately goes to with this situation. You may want to do something a little or completely different, but I hope this helps!
EDIT: BTW, here are some resources on the concepts that I used in this answer:
http://unity3d.com/learn/tutorials/modules/beginner/scripting/arrays http://unity3d.com/learn/tutorials/modules/beginner/scripting/enumerations http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops http://unity3d.com/learn/tutorials/modules/intermediate/scripting/ternary-operator
Complete mess ain't it lol, that's why i stopped and had to ask here for help before it got out of control haha
Thanks allot for the help, $$anonymous$$uch appreciated.
Your answer

Follow this Question
Related Questions
Convert this line of javascript to C# (easy) 1 Answer
i need some code suggestions 1 Answer
how to stop animation loop? 1 Answer
oescape script name? 1 Answer
Stand alone player 0 Answers