Open Source Particle System

From Unify Community Wiki
Revision as of 10:59, 26 July 2011 by Diviner (Talk | contribs)

Jump to: navigation, search

Author: Nick Tziamihas



The particle system integrated in Unity3D, although conclusive, does not expose every variable and method tied to particle generarion and animation. This makes particle system modifications difficult and some times impossible. In this project, we create our own particle system from scratch, and expose all of its aspects to the developer.



This sample particle system creates an ellipsoid particle emitter that can be attached to any Game Object. The sample allows single direction movement, particle color animation, billboard setting, particle speed, lifetime, quantity and localScale trasnformation. Additional features can be added manually, as needed.


Create an empty object and name it particleEmitter . Attach on it the EmitterController.js script. Create a plane Object, remove its Colider, and name it particle . Assign to the object the material needed for the new particle. Attach on the particle object the ParticleController.js script. Create two prefabs called particleEmitter and particle, and drag the two Game Objects created above to each respective prefab. Select the particleEmitter prefab, and from the Inspector, drag the particle prefab to the Particle Mesh entry. Set up both prefabs in the inspector to achieve the wanted particle effect as you would in Unity's default particle system. To use the new system, simply drop the particleEmitter prefab in your scene and place it wherever you want a particle system to generate.


Make sure that the Particle Life value in both particleEmitter and particle prefabs is the same number. Accessing external components in Unity can be slow, especially in particle systems, and therefore Particle Life must be set twice, in both prefabs, in order to achieve fluid particle animation. Also note that in order to remain fully independent from source level code, this particle system does not use Physics for movement (in order to keep the deltaTime open for modification).

Javascript- EmitterController.js

<javascript> private var myTransform : Transform; var particleMesh : Transform; //Drag here the particle prefab var emissionArea : float; //The area around the particle emitter where particles spawn (set as range around the emitter's location) var particleAmmount : int; //The ammount of particles to be spawned in every pass var particleLife : float; //The duration of each particle's life in seconds

function Start () {

       myTransform = transform;

initiateEmition(); }

function initiateEmition() { var timeStep : float = particleLife / particleAmmount; var loopHandle : boolean = false; while(!loopHandle) { var newParticle = Instantiate(particleMesh,myTransform.position * (Random.Range(1,emissionArea +1)), Quaternion.identity); yield WaitForSeconds(timeStep); } } </javascript>

Javascript- ParticleController.js

<javascript> private var myTransform : Transform; private var mainCamera : GameObject; //The Main Camera is used for the billboard setting. If you change the Main Camera's name, edit the refference below private var myEmitter : GameObject; //The particle Emitter. If you change the Emitter's name, edit the refference below var particleSize : Vector3; //The size of the particle's localScale in x,y,z var particleDirection : Vector3; //The direction of the particle's movement in local space var particleLife : float; //The duration of the particle's life in seconds var animateColor : boolean = false; //Whether color animation is wanted or not var animationColor : Color[] = new Color[5]; //Default color animation passes through 5 steps. Increase the number for more fluid animations. var billboard : boolean = true; //Whether the particle faces the camera at all times or not

function Start () { myTransform = transform; mainCamera = gameObject.Find("Main Camera"); //Change this name if you have renamed your Main Camera myEmitter = gameObject.Find("particleEmitter"); //Change this name if you have renamed your particleEmitter myTransform.localScale = Vector3(particleSize.x, particleSize.y, particleSize.z); if(animateColor) initiateAnimation(); Destroy(myTransform.gameObject, particleLife); }

function Update () { if(billboard) myTransform.LookAt(mainCamera.transform.up); myTransform.Translate(particleDirection * Time.deltaTime, myEmitter.transform); }

function initiateAnimation () { var timeStep : float = particleLife / animationColor.length; for(var i : int = 0; i < animationColor.length; i++) { renderer.material.SetColor("_TintColor", animationColor[i]); //This is for particle shaders that use _TintColor instead of _Main

               renderer.material.color = animationColor[i];

yield WaitForSeconds(timeStep); } } </javascript>

Personal tools