I heard about #1GAM before but I never had the time or drive to participate. I decided to enter something, no matter how small for May 2014. May’s optional theme was MONEY. Since time was short, I went with the first thing I thought of: (even though normally this is bad, but this would also stop me from second-guessing myself) use money to buy platforms. I thought this was a novel idea.
I downloaded Unity’s 2D Platformer to give me a nice base to work with. I don’t know a ton about Unity’s 2D options (although the new GUI stuff coming on in 4.6 looks awesome) but was willing to learn, even though I didn’t get to learn as nearly as much as I wanted to. I was hoping to work with Tom Snively for music and SFX, but he ended up being busy. (the good kind)
Tom found a nice SFX for getting money and I used SpriteLib for graphics because it’s great and free for very small games like this. Tom set up our project in Dropbox, although I’m not a fan since it can cause issues with Unity.
My original idea consisted of having some sort of interface to pet the player choose what kind of platform they wanted to buy and thus place it in a predetermined spot in the level, but as time passed I wanted to get something up on the screen as quickly as possible. Thus, I needed to just say “if you have x amount of money, show this platform, and if you have greater than y, show that one.” Easy enough to program.
However, I ran into a bump in the road so I contacted Mike Croswell, a great fellow indie dev. He’s a Unity master. He even has a book coming out! (Go Mike!) So what was my issue? Well, you can use FindGameObjectsWithTag() to get all of the game objects that are tagged with a specific tag in a scene, (whodathunk?) but the shitty thing is is that you can’t control in what order Unity grabs the objects. I wanted to use an array and call them in a specific order, but even with the new Hierarchy sorting in Unity 4.5, Unity’s just gonna do whatever it wants to do. Yuck. Hopefully it’ll be made more logical in a future version. Mike suggested just using the GUI to specify the exact objects, but I was a little wary as Unity can lose object associations. After I expressed this concern to Mike, he reassured me that this is a rare occurrence and even though my method was the most sound, we needed to work around Unity’s limitation somehow. In my code:
[csharp]
//Unity sucks at stuff:
//http://answers.unity3d.com/questions/696518/array-loads-in-wrong-order.html
[/csharp]
I also remembered a trick I learned from a Unity tutorial: keep an array of collectible objects. When working with this tutorial, I wanted to reset all the cubes by making them active again, but it seems FindGameObjectsWithTag() doesn’t return objects that are inactive, so there’s no easy way to get them again if they become inactive. (that I know of) So, in Awake(), you make an array to keep track of everything:
[csharp]
greenMoneyArray = GameObject.FindGameObjectsWithTag("GreenMoney");
[/csharp]
Lastly, here are some other tips I came across while coding: (see the comments in the code)
[csharp]
void OnTriggerEnter2D(Collider2D weTouched){
//Use lowercase gameObject to get the actual game
//object attached to the collider
if(weTouched.gameObject.tag == "GreenMoney"){
//We can’t play the audio clip DIRECTLY from the object
//e.g., (weTouched.audio.Play();) as it will get disabled
//as soon as the player picks it up (next line)
//"clip" is built in
AudioSource.PlayClipAtPoint(weTouched.audio.clip, transform.position);
weTouched.gameObject.SetActive(false);
collectedMoney += 1;
moneyCountTxt.text = "Money: $" + collectedMoney.ToString();
//If you’re cool enough to get money, you already know the controls
controlTxt.text = "";
//controlTxt.enabled = false; works too
}
if(weTouched.gameObject.tag == "WinItem"){
AudioSource.PlayClipAtPoint(weTouched.audio.clip, transform.position);
weTouched.gameObject.SetActive(false);
//We win!
winTxt.text = "YOU WIN!\nThank you for playing!";
}
}
[/csharp]
While talking with Mike about the other issue I was having (our player getting stuck on walls) we came to the conclusion that Unity’s physics system sucks sometimes and I wasn’t able to solve that in time before the month ran out. Oh well. Unity in their 2D Platformer demo used a kludgy (thanks for that word Mike) method to stop this, but I was wondering if there was a way to avoid this. Mike surmised that the player’s movement would need to be done another way for this to be feasible.
I wish I had more time in the month to replace the main character with something else, learn Unity’s animation system, or add more gameplay, but I got done what I got done.
That’s all for me now folks. Feel free to play the game below and download the source to learn from it.
Play Online: Money Game
Download: Money Game Source