Random Code: Bottomless Pits

Feeling random today, so I’m starting a new series of posts where I give examples of the code I have used in my games, written in Javascript, and for use with Unity. Today I will post some code that will run when the player falls off a level. First you need a collider of some kind placed below the level; make it wide enough so that your player always lands on it when they fall of the level, and as far down as you want (but not so far it will take ages to fall to it). Make sure the collider is a trigger, then attach to it a script containing this code:

function OnTriggerEnter(other: Collider)
    if(other.tag == "Player")
        other.SendMessage("Die", SendMessageOptions.DontRequireReceiver);

This code basically checks if the collider falling into the trigger is tagged “Player”, and if so, it will send the message “Die” to the object, which will call the Die() function in any scripts attached to the player; this is useful for putting some respawn code in the Die() function, so the player gets respawned whenever they fall off the level. If the object isn’t tagged “Player”, then it simply gets destroyed, so enemies and scenery will just disappear rather than fall indefinitely. Just remember to tag your player as “Player”, and then you’re good to go! Also, it would help if someone could tell me why WordPress is colossally bad at letting me indent text, it was a pain to type this post, but I hope it helps anyway.


I’m also making progress with Project Spikes, and another update will come around pretty soon. For now, a teaser image!

spikes-11Yeah, grayscale images ftw! Anyway, happy coding peoples!




04/05/13 Update

I’ve done more tweaking of the gems, and I’ve now added some sparkles to each gem, to make them a bit more pretty. I’ve also added one more gem, the diamond.unity_gems_5These are really sparkly, and this is something I will transfer to all the other gems next. The shape of the diamonds is quite cool, as the light reflects off them in a really cool way. Diamonds are worth 10 gems, making them extremely valuable to collect, but also they will be rarer in the final game. This now means there are 5 gems with the upgraded models I’m using.

unity_gems_all_2Another thing I’ve done is to upgrade the colliders of the gems from sphere colliders to mesh colliders. With the old sphere colliders, gems would occasionally roll around indefinitely and fall off the level, but now they will rest on their edge, which is a lot more realistic and looks much better.

unity_platformer_11This means gems can be found on their side or upside-down, although the default position of the gems in the game will be face-up. All this means is that they can be deflected across the level from an explosion and land in a realistic position. The way I tested this was by putting many gems in two containers,  with one container filled with gems with the sphere collider, the other with mesh colliders, then starting the game. As they fall from the container, the sphere collider gems roll out in all directions indefinitely, but the mesh collider gems land in a neat pile.

unity_platformer_12This works really well, and now all the gems have mesh colliders. You can see this happen in the YouTube video I produced about this update. I will start to do video updates along with some of these blog updates, to show what I mean in more detail. Download the game here and give it a go, feel free to give feedback or ideas what I could do next. I will be working on trees later, so there should be another update relatively soon. That’s it for today, thanks for reading.


Day 13 Progress

This is the download link to the latest version of the game:


I’ve been working on making new terrain for the levels in my game. The levels are a bit basic at the moment, and they don’t have many features, but they are much more interesting than what I had before. I have been experimenting with heightmaps and the Unity terrain generation to get better level terrain, and I am happy with the result. I had a few problems initially because using heightmaps in Unity requires .raw files, which are basically image files with very little formatting, and I am using GIMP, which cannot save to a .raw file. I found a program that can convert png files into raw files though, so I got it working eventually. Here’s a tip: if you have Photoshop, use that, it’s as easy as just saving to a .raw file…  hub_world_1

This is the heightmap I used for my hub world. I got this through a quick Google image search but you may want to create your own, like I did for my other levels.

A heightmap is basically a top-down greyscale view of your terrain, with white areas highest up and black areas lowest down, and grey in-between. When this is imported into Unity using the Terrain feature, it automatically creates terrain to match your heightmap. That makes it much easier to create your levels. You can then ‘paint’ your terrain with any texture you want, and bam! You have your basic level. Then you can just add objects in, like scenery, enemies and collectibles, and you have a game. I need to add some enemies and scenery, but I’m happy with my current progress.

I have also been working on getting my character to pick up gems, and it finally works! There is no counter to actually keep track of how many you’ve picked up and they have no function yet, but this will be implemented soon. The way I fixed it is by adding the ‘Rigidbody’ component to the box collider my gem pickup script was attached to, as this allows the ‘OnTriggerEnter’ function in the script to work. Basically this function is activated when any object with a collider component enters the collider of the object I’ve attached the script to, in this case the player. When activated, the script checks for any objects with a “Gem” tag, and destroys it from the game completely. So now you can pick up gems. Yay!


This is the GameObject that handles all the gem grabbing.

I have also made a very simple start screen with two buttons, one to enter the game and another to quit the game. This is made with Unity’s built-in GUI creator. In this title screen, you can also walk around with the player, a feature I want to keep in the final version. It looks like this: unity_platformer_4

When you press the “Quit Game” button, the application will simply close. But when you click the “New Game” button, it will enter the game, starting with the hub world. This has had a few minor changes too: unity_platformer_5

The terrain for this world was made with the heightmap above, and it made a level with a couple interesting mountains as well as a large flat land in the middle. The textures are just default textures provided with Unity, but I will change them to something I like a bit more at a later date. I’ve also added a skybox, which is basically an imitation of a sky, but the camera angle doesn’t let you see it very well. There is a portal in this world leading to “Level 1”: unity_platformer_6

This is supposed to be a desert-like level, but I haven’t had much time to work on it so the textures aren’t complete. The heightmap for this one was entirely made by me though. I’m very happy with the progress I’ve made here, and I hope you will all have fun playing around with this, and if you find any problems or anything you would like added, please don’t hesitate to comment below. Thanks for all the support.