Why doesn’t my particle system show in front of my sprites??

Particle System Sort LayerThis one had me befuddled for a little while. I could not figure out why my particle system was not showing up in front of my sprites for the life of me. I had the “Layer” set properly but I could not find a place to set the “Sorting Layer”. I came across some other folks who were saying you had to set the sorting layer manually in code so the particle system would show up where it needed to (sorry, I can’t remember the references!). Okay, that seems easy enough, let’s see if we can figure that out. It looks like we can do that via the following code. The “sortingLayerName” is just a string of a sorting layer you have defined in Unity and the “sortingOrder” is an integer that specifies the z-order in that sorting layer. The higher the number, the closer the object “looks” to you.


particleSystem.renderer.sortingLayerName = sortingLayerName;
particleSystem.renderer.sortingOrder = sortingOrder;

So now let’s just make this into a script so we can easily apply it to all particle systems we create.

I put the following code into a file named “ParticleSystemFix.cs” and attached it to any particle systems I create.

using UnityEngine;
using System.Collections;

public class ParticleSystemFix : MonoBehaviour {

public string sortingLayerName;
public int sortingOrder;

/**
 */
void Start() {
    particleSystem.renderer.sortingLayerName = sortingLayerName;
    particleSystem.renderer.sortingOrder = sortingOrder;
    enabled = false;
  }
}

I’ll usually tell a script to disable itself once it is done initializing or if the code in the Update() function starts to be skipped because of a certain condition. I’m sure there has to be a slight performance advantage to doing this…every little bit helps.  Plus it lets me know if something is executing properly when I expect the script to become disabled.

Particle System Sort LayerNow, just attach this script to any particle system; there in the inspector, you can define a “sortingLayerName” and “sortingOrder” on the “ParticleSystemFix”.  I used a large number like 999 as my “sortingOrder” since I want my particle systems to appear on top of all my sprites.

 

 

Particle System Sort LayerMake sure that what you pass into the “sortingLayerName” is defined in Unity!

That is about it, as long as the “sortingLayerName” for your particle system is where it needs to be hierarchically, your particle system should appear on top of the sprites.

 

 

 

 

Unity what??

“So what’s this ‘powered by Unity’ screen I keep seeing when I play your games?”

I’ve never been more excited about game development than after discovering a little tool by the name of Unity3D about 4 months ago…and I use the term “little” very loosely. Why am I so excited about it? It takes a lot of the meticulous initialization and setup you used to have to do through code and handles a lot of it for you so you can focus on the code that handles the content and game play. Back in the days of C++ (am I showing my age now?) to do something like a simple animation, you had to load the sprite sheet, determine the location and size of each frame of the animation on the said sprite sheet, create a mask so the transparent parts of the image would be transparent and create an engine that would cycle through those images smoothly.  Yes, the “BitBlt” function was my friend during those times. That doesn’t even include the collision detection on the sprite itself! This was done ALL through code and was exceptionally tedious. Unity3D gives you the ability to do all of this without writing a single line of code.  Absolutely amazing. You can now focus your code on what your character DOES instead of how to just make your character appear on screen and walk. And that’s just scratching the surface, there is so much more to Unity3D that I haven’t even touched on yet. Oh, did I mention it also has this nifty feature for building your game on multiple devices and platforms fairly easily? How else could we get our games out for Android and iOS so quickly?  I’d really love to put together a 3D game sometime but I’ll have to get more familiar with Blender for that one. “Ummm…weren’t you just talking about Unity3D? What’s ‘Blender’?” I’ll save that one for another time.

So what exactly does Unity3D look like in action? Glad you asked 🙂 One of the powerful features of Unity3D is it allows you to “play” the game during development so you can see what each object is doing and how they are reacting to their surroundings. Debugging on steroids is what I call it. It also REALLY helps to have two monitors. If you are interested in game development, I HIGHLY suggest getting Unity3D and tinkering with it.

Unity3D - Shape Sprout
Unity3D – Shape Sprout

Teaching a monkey to fly

There is a LOT that goes into the development of an app of any kind. Especially games. You’ve got to put together a plan of what your game is going to do and how it’s going to do it, figure out what kinds of media you will need for it, then collect or create the graphics, sound effects and music then FINALLY you get around to the fun part…the coding! For just two people, that is a lot of work. My strength tends to lie in the logical and less so on the artistic side. Luckily, we have the awesome music of Kevin MacLeod available to use, royalty free might I add, in our games. It’s the same with the sound effects from freeSFX. Although, finding the right music and sound effects to match the mood can be a bit of a challenge. So that leaves just the graphics…did I mention my strength is NOT in the arts? I’ve learned a lot about animations and whatnot over the last few months as our new game Shape Sprout has a TON more graphics than Shape Sense…by a long shot. I liken this to teaching a monkey to fly. Most people would probably say the monkey is falling. I prefer to take the more positive stance that he’s not falling; more like he’s falling rather gracefully.