Tag Archives: iDevBlogADay

Make A 2D Game With Unity3D Using Only Free Tools Part 3

Welcome to Part 3 in this series on making a 2D sprite-based game with Unity 3D. In Part 1 I introduced you to the tools we’ll be using to make a Lode Runner style action game and in Part 2 we created the level sprites and built our first level. In this tutorial we’re going to be adding in the player, hooking up the scripts and adding collision to the ladders and ropes so that you’ll finally be able to run the character around in your levels.

This tutorial assumes that you have already followed along with Part 2 of this series so your project should be ready to continue on with the steps below. If you haven’t already done Part 2, you should go back and do that first so you can get a full understanding of how everything fits together. If you’d rather skip ahead, you can download the project up to this point. You can also click here to see how the game will look at the end of Part 3.

Making the Player Sprite Atlas

If you’ve been following the series, I know you’re excited to get the player character running around in the game, so the first thing we need to do is create a sprite atlas in TexturePacker.

  • Download the source sprite .png’s and unzip the file somewhere on your hard drive. If you already downloaded the sprites and added them to your project in Part 2, then you can skip this step.
  • Launch TexturePacker and then drag & drop all of the .png files from the sprites/player folder into the Sprites panel.

Texture Settings / Layout:

  • Set Algorithm to Basic
  • Set Border Padding to 1
  • Set Shape Padding to 1
  • Uncheck Trim

Texture Settings / Output:

  • Leave the Data format set to cocos2d.
  • Under Data File, click the little “…” button and browse to the location in your project’s Asset folder where you want to store your sprites (I put mine in Assets/SpriteAtlases), name the file “player” and then click Save.
  • TexturePacker automatically adds the .plist extension to the Data File, but Unity wants the file to be .xml. So in the text field, replace .plist with .xml.
  • The Texture File path should already be set to the same location as the .xml file except that it will have a .png extension so there’s nothing to do there.

If you followed the steps above, then your settings in TexturePacker should look like this (click the image to see a larger version):

Now if you click the Publish icon in TexturePacker and then switch back to Unity, you should see a player.png file and a player.xml file in the SpriteAtlases folder in the Project view.

If you don’t already have the project open in Unity, open it now and then load the scene that you created in Part 2 (eg. level1.scene).

We need to make a couple of changes to the sprite atlas in Unity so that it looks correct.

  • Select the player.png file in the Project tab. In the Inspector change the Filter Mode to Point.
  • Click the Override for Web box, set the Format to Truecolor and then click Apply.

Making the Player Sprite

Now we’re ready to turn the player’s sprite atlas into animated sprites using the Orthello 2D plugin.

The Sprite Container:

  • In the Unity Project tab, expand the Orthello folders: Orthello –> Objects –> Sprites –> SpriteAtlas and then drag the SpriteAtlas-Cocos2D object into the Hierarchy.
  • In the Hierarchy tab, expand the OT object and then the Containers object and you will see your new container with a name something like “Container (id=-6840)“. This is the Container that will hold all of our player sprites from the atlas we made so you can rename the Container to something obvious like “player“.
  • Drag the player.png from the SpriteAtlases folder and drop it on the “OTSprite Atlas Cocos 2D” script’s Texture slot.
  • Drag the player.xml from the Project, SpriteAtlases folder and drop it on to the Atlas Data File slot. Now if you drop down the little Atlas Data arrow, you should see that it’s populated with all the sprite atlas data that TexturePacker generated for us.

Setting Up The Player Animations:

Now we need to assign all of the frames from the sprite atlas to individual animations.

  • Drag an Animation object from Orthello –> Objects –> Sprites into the Hierarchy. This will add a new object under OT –> Animations named something like “Animation (id=-4320)“. Rename this object to “player anims“.
  • With the new “player anims” OTAnimation still selected, adjust the settings to match those in the following image. To populate the Container field, drag & drop the “player” object from OT –> Containers on to the Container field.

Click the image to see all of the animation settings:

Making The Player Sprite

  • Next find the AnimatingSprite object in Orthello –> Objects –> Sprites object in the Project tab and drag it into the Hierarchy, this will make a new object in the scene with a name like “Animating Sprite (id=-23050)“. Rename this object “player“.
  • With the new player object still selected in the Hierarchy, drag the “player anims” object on to the Animation slot. The Sprite Container slot should automatically fill with a reference to the “player” container object, if it doesn’t you can drag & drop that onto the slot.
  • Now you should see the player sprite in your scene and if you press Play in Unity, the sprite will animate through all of the frames in all of the animations. We don’t want the animation to play on start so uncheck the Play On Start checkbox.

Adding Collision To The Player

In order for the player to collide correctly with the ladder and rope colliders we’ll be creating later, we need to set the following.

  • With the the player “OTAnimating Sprite” still selected in the Hierarchy, check the check box next to Collidable. This will automatically add a Rigidbody component to the object.
  • Click the dropdown list next to Physics and select Custom from the list.
  • Under Transform, set the Scale Z to 1.
  • Change the Depth to -1.
  • Under Box Collider, set Center Y to -0.1 and Z to 1. Change Size X to 0.45, Y to 0.6 and Z to 0.4.

If you followed the steps above, the settings on the player sprite should look like the following image.

Click on the image to see all of the settings:

Changing the Depth to -1 and the Center Z to 1 on the Box Collider will position the Collision at 0 on the Z axis while moving the player sprite 1 unit towards the camera. This has 2 effects: it makes sure that the player will always be visible in front of the level sprites while keeping the collision at 0 on the Z so that it will collide with the ladder and rope triggers. It sounds kinda strange, but you can see how it should look in this screen shot:

2dGamePt3 Player Collision

Setting Up The Shooting Animation

We need to add another animating sprite so that when you press the fire button, you’ll see a bullet blob animate from the player and hit the ground.

The Shoot Animation:

  • Drag an Animation object from Orthello –> Objects –> Sprites into the Hierarchy. This will add a new object under OT –> Animations named something like “Animation (id=-4320)“. Rename this object to “shoot anim“.
  • With the new “shoot anim” OTAnimation still selected, adjust the settings to match those in the following image. To populate the Container field, drag & drop the “level” object from OT –> Containers on to the Container field. Remember that we already added the shoot sprite animation to the level sprite atlas in Part 2 of the series.

2dGamePt3 Shoot Sprite Animations

The Shoot Sprite

  • Next find the AnimatingSprite object in Orthello –> Objects –> Sprites in the Project tab and drag it into the Hierarchy, this will make a new object in the scene with a name like “Animating Sprite (id=-23050)“. Rename this object “shoot“.
  • With the new shoot object still selected in the Hierarchy, drag the “shoot anim” object on to the Animation slot. The Sprite Container slot should automatically fill with a reference to the “level” container object, if it doesn’t you can drag & drop that onto the slot.
  • Set the Transform Position X to -1 so that the sprite is position to the left of the player’s sprite.
  • Set the Depth to -1 so that the sprites will appear in front of the level sprites.
  • Set the Frame Index to 18.

Now you should see the shoot sprite in your scene and if you press Play in Unity, the sprite will animate through all of the frames in all of the animations. We don’t want the animation to play on start so uncheck the Play On Start checkbox.

We only want to see the shoot sprite when the player is actually shooting so uncheck the checkbox next to Mesh Render on the shoot sprite. This will hide the sprite in the scene until we show it again later with a script.

Parenting The Shoot Sprite

We want the sprite to move with the player and we also need to flip the sprite so that it’s either on the player’s left or right side depending on which way the character is facing.

  • Go to Game Object –> Create Empty
  • Rename this object to “shoot parent” and make sure the X, Y, Z Position on the object is set to zero.
  • In the Hierarchy, drag and top the shoot sprite on to the shoot parent so that the shoot sprite becomes a child of the shoot parent object.
  • Drag and drop the shoot parent object on to the player sprite so that it’s a child of the player.

If you followed all the steps so far, your Hierarchy should look something like the following image. Note that I put all of my level tiles under an empty game object named LEVEL to keep things organized.

2dGamePt3 Player Parenting

Note that in order for the scripts to work, the child objects under the player must be named exactly as shown in the above image.

Hooking Up The Scripts

We’re finally ready to add the scripts so that the player can move!

  • Download the player scripts and unzip the file somewhere on your hard drive.
  • Drag the Scripts folder from the .zip into your project’s Asset folder.
  • Create an empty game object by going to Game Object –> Create Empty
  • Rename the new object to something like “Scripts“.
  • Drag and drop the xa.cs file from the Project tab onto the Scripts object in the Hierarchy.
  • Drag and drop the player.cs and the playerAnims.cs files from the Project tab on to the player object in the Hierarchy.
  • Make sure that the player is positioned above a brick tile so that she has something to stand on. Remember that it’s the player’s Box Collider that actually collides with objects in the world and not the visible sprite object, so the box will be positioned at something like 0,0,-1 (X and Y can be anywhere in the level but Z should always be -1).

Now if everything is setup correctly, when you press Play in Unity the player should stand on a brick tile without falling through it. And if you press left and right on the keyboard the player should move and the run animations should play.

Describing how the scripts work is beyond the scope of this tutorial, but I did add comments to the scripts that should hopefully help you understand how everything is working. If you have questions about anything in the scripts, please feel free to ask in the comments below or email me directly.

Adding The Ladder Colliders

In order for the ladders to work, we need to add colliders to them.

Creating A Ladder:

  • Go to GameObject –> Create Other –> Cube
  • Rename the Cube to something like “Ladder“.
  • Open the Tag Manager by going to to Edit –> Project Settings –> Tags
  • Under Tags in the next available slot (probably Element 1) type “Ladder” and then in the next slot after that, type in “Rope” (without the quotes).
  • While we’re here, we need to add another layer. Under the next empty User Layer (probably User Layer 9), type “NoDraw” (without the quotes).
  • Now click on the Ladder cube you created before and change the Tag to “Ladder” and then change the Layer to “NoDraw“.

Hiding Objects In The Game View:

You’ve probably noticed that the Ladder collider is visible in the Game view but we don’t want to see these in the actual game. To hide them, we need to make a change to the camera.

  • Click on the Main Camera object in the Hierarchy.
  • Under Camera, drop down the list next to Culling Mask and click on “NoDraw” to deselect it. Now any object that is in the “NoDraw” layer will be hidden in the Game view.

Sizing And Positioning The Ladder:

Every ladder in the scene needs to have one of these Ladder colliders and we need to adjust the size of the Ladder colliders to match the height of each of the ladders in the level. The ladder collider needs to be 1 unit taller than the visible ladder – so if your ladder is 4 tiles (sprites) tall, then the collider needs to be 5 units tall.

  • Assuming your ladder is 4 sprites high: Select the Ladder collider and change the Scale Y to 5 (1 unit taller than the visible ladder).
  • Snap the Ladder collider to one of the lower corners of the bottom sprite using the Vertex Snap feature in Unity.
  • You can duplicate this object, resize the Y and snap it to all the other ladder sprites in your level.

Your ladder should look like this (click to see a larger version):
2dGamePt3 Ladder Trigger

Now if you press Play in Unity and then walk the player over to the ladder, she should be able to climb up and down the ladder, the player’s sprite should be playing the climb animation and you should be able to exit the ladder at the top and bottom levels and also fall off the ladder if you exit somewhere in the middle.

Adding the Rope Colliders

The rope colliders follow pretty much the same setup as the ladders.

Creating A Rope:

  • Go to GameObject –> Create Other –> Cube
  • Rename the Cube to something like “Rope“.
  • Change the Tag to “Rope” and then change the Layer to “NoDraw“.

Sizing And Positioning The Rope:

Like the Ladders, every rope in the scene needs to have one of these Rope colliders. But unlike the Ladders, the Rope colliders must be the same width as your rope sprites.

  • Assuming your rope is 4 sprites wide: Select the Rope collider and change the Scale X to 4 (Y and Z scale should be 1).
  • Snap the Rope collider to one of the lower corners of the bottom sprite using the Vertex Snap feature.
  • You can duplicate this object, resize the X and snap it to all the other rope sprites in your level.

Your rope should look like this (click to see a larger version):
2dGamePt3 Rope Trigger

Now if you press Play in Unity and then walk the player over to the rope, she should be able to climb left and right along the rope, you should see the rope hang animation playing and if you press the down arrow while hanging from the rope, she should let go of the rope and fall.

Conclusion

Now you have everything you need to make levels that support the player’s full suite of movements: running, ladder climbing and rope shimmy. In the next tutorial, we’re going to be adding the scoring system, breaking out brick tiles and picking up objects.

You can download the project up to this point and you can play the web version of the project here.

If you like this post, please be sure to say hi in the comments and follow me on Twitter and Facebook. Your support helps to keep these tutorial coming. This blog post is part of iDevBlogADay, a collection of indie developers writing about their development experiences.

Regarding The AI:

When I began this series, It was my intention to include enemies that could follow the player around the level using behaviors similar to the original Lode Runner game. However my time has become increasingly limited and my programming skills aren’t fully up to the task of recreating their AI so I might not be able to get AI into this the series.

If you are a programmer who would like to contribute an AI behavior solution to this tutorial series, please contact me.

More Tutorials In This Series

Make A 2D Game in Unity3D Using Only Free Tools Part 1
Make A 2D Game in Unity3D Using Only Free Tools Part 2
Make A 2D Game With Unity3D Using Only Free Tools Part 3
Make A 2D Game With Unity3D Using Only Free Tools Part 4
Make A 2D Game With Unity3D Using Only Free Tools: Beginning Enemy AI With A* Pathfinding

And here’s another tutorial series that uses Sprite Manager 2 for the sprite display and animation duties:
Creating 2D Games with Unity3D Part 1
Creating 2D Games with Unity3D Part 2
Creating 2D Games with Unity3D Part 3
Creating 2D Games with Unity3D Part 5

Show Your Support

If you find these tutorials useful, please considering buying one or more of our games and apps for iPhone and iPad. Your support helps to keep the tutorials flowing!

  • Giant Moto: High flying arcade style motocross action!
  • Small Space: Fly a spaceship, eat space creatures, score point!
  • Holeshot Drag Racing: High speed drag racing action!
  • Alien Booth: Transform pictures of your friends into awesome aliens!
  • iSpoof Walken: A hilarious look into the world of Christopher Walken!
  • Gaga Eyes: Transform pictures of your friends to give them huge anime eyes!

About the author

Tim MillerTim Miller is a veteran game developer with over 15 years experience in the video game industry working as a Level Designer and Lead Level Designer at LucasArts, Secret Level and Nihilistic Software. He co-founded Rocket 5 Studios with his wife Cathy and their dog Rocket in 2009. Twitter, LinkedIn, Portfolio

Make A 2D Game with Unity3D Using Only Free Tools Part 2

Welcome to part 2 of this tutorial series on making a Lode Runner-style 2D game with Unity 3D. In part 1, I introduced you to a bunch of free tools and scripts that we’ll be using and showed you how to setup your project. In this post, I go over how to create the level sprites and build your first level.

Of all the Unity plugins we covered in part 1, the main plugin we’ll be focusing on in this installment of the series is Orthello 2D. Orthello was recently updated with a lot of cool new features (as I write this, 1.6a is the latest version) so make sure you’re using version 1.6a or newer.

Initial Project Setup

We need to make a few changes to the project settings before we start making the sprites.

Build Settings:

  • File –> Build Settings…
  • Click on Web Player and then click Switch Platform. (You could leave this set to PC and Mac Standalone if you prefer.)
  • Close the Build Settings window.

Player Settings:

  • Edit –> Project Settings –> Player
  • Under Per-Platform Settings click on the little world icon (assuming you set your platform to Web Player in the previous step).
  • Click on Resolution and Presentation and change the Screen Width to 800 and the Screen Height to 600.

Render Settings:

Since you’re typically not going to use Unity’s lighting system in a 2D sprite-based game, we want to brighten things up in here by adjusting Unity’s default ambient light. This step is optional.

  • Edit –> Render Settings…
  • Click on Ambient Light and change the color to white (255, 255, 255, 255).

Initial Orthello 2D Setup

  • In the Unity Project tab, go to Orthello –> Objects and then drag the OT object into either the Scene or the Hierrachy tab.
  • In the Hierarchy tab, drop down the little arrow next to the OT object and then click on View.
  • Change the Pixel Perfect Resolution to 800 x 600 (same as we set for the Player Settings)
  • Change the Custom Size to 10

Now if you select the Main Camera in the Hierarchy, you’ll see that Projection is set to Orthographic and Size is set to 10. Orthello automatically changed the Projection from Perspective (Unity’s default setting) to Orthographic when you added the OT object into the scene. And When we changed the Custom Size on the Orthello View object to 10, it set the Main Camera’s Size to 10.

With a bit of experimenting, I found that with a screen resolution of 800×600 and an Orthographic Size of 10, a Cube at scale 1x1x1 will be exactly 30 pixels on screen which happens to be the exact size of the sprites we’re going to use to build the levels which should make it easy for us to stick to a grid when building levels.

At this point, your project should look something like the following image (click to see a larger image). Note that I also added a Cube to the scene for scale comparison.

Download the project up to this point.

Making The Level Sprite Atlas

Ok now that all that initial setup stuff is out of the way, it’s time to dig in and have some fun. The levels in Lode Runner were built using just a few simple tiles: Brick (digable), Concrete (un-digable), Ladder and Rope.

  • Download the source sprite .png’s and unzip the file somewhere on your hard drive.
  • Launch TexturePacker and then drag & drop all of the .png files from the sprites/level folder into the Sprites panel.
  • Then select all of the .png files in the sprites/shoot folder and drag those into the Sprites panel too.

Texture Settings / Layout:

  • Set Algorithm to Basic
  • Uncheck Trim
  • Uncheck Enable Auto Alias.

Texture Settings / Output:

  • Leave the Data format set to cocos2d.
  • Under Data File, click the little “…” button and browse the location in your project’s Asset folder where you want to store your sprites (I put mine in Assets/SpriteAtlases), name the file “level” and then click Save.
  • TexturePacker automatically adds the .plist extension to the Data File, but Unity wants the file to be .xml. So in the text field, replace .plist with .xml.
  • The Texture File path should already be set to the same location as the .xml file except that it will have a .png extension so there’s nothing to do there.

If you followed the steps above, then your settings in TexturePacker should look like this (click the image to see a larger version):

Now if you click the Publish icon in TexturePacker and then switch back to Unity, you should see a SpriteAtlases folder in the Project tab with the sprite atlas and a sprite data files inside.

We need to make a couple of changes to the sprite atlas in Unity so that it looks correct.

  • Select the level.png file in the Project tab. In the Inspector change the Filter Mode to Point.
  • Click the Override for Web box, set the Format to Truecolor and then click Apply.

Making The Level Sprites

Now it’s time to dig into Orthello and turn the atlas into sprites.

The Sprite Container:

  • In the Unity Project tab, expand the Orthello folders: Orthello –> Objects –> Sprites –> SpriteAtlas and then drag the SpriteAtlas-Cocos2D object into the Hierarchy.
  • In the Hierarchy tab, expand the newly created OT object and then the Containers object and you will see your new container with a name something like “Container (id=-6840)“. This is the Container that will hold all of our level sprites from the atlas we made so you can rename the Container to something obvious like “level“.
  • Drag the level.png from the Project, SpriteAtlases folder and drop it on the “OTSprite Atlas Cocos 2D” scripts Texture slot.
  • Drag the level.xml from the Project, SpriteAtlases folder and drop it on to the Atlas Data File slot. Now if you drop down the little Atlas Data arrow, you should see that it’s populated with all the sprite atlas data that TexturePacker generated for us.

Making An Animated Brick Tile:

The brick needs to have some animations on it that will play when it’s destroyed and when it regenerates so we need to make an Animation.

  • Drag an Animation object from Orthello –> Objects –> Sprites into the Hierarchy. This will add a new object under OT –> Animations named something like “Animation (id=-4320)“. Rename this object to “level anims“.
  • With the new OTAnimation still selected, adjust the settings to match those in the following image.
  • Under Framesets, set the Size to 3
  • To populate the Container field, drag & drop the “level” object from OT –> Containers on to the Container field.

  • Next find the AnimatingSprite object in Orthello –> Objects –> Sprites and drag it into the Hierarchy, this will make a new object in the scene with a name like “Animating Sprite (id=-23050)“. Rename this object “brick“.
  • With the new brick object still selected in the Hierarchy, drag the “level anims” object on to the Animation slot. The Sprite Container slot should automatically fill with a reference to the “level” container object, if it doesn’t you can drag & drop that onto the slot.

Now you should see a brick sprite in your scene and if you press Play in Unity, the sprite will animate through all of the frames in the animation. We don’t want the animation to play on start so uncheck the Play On Start checkbox.

Note: if you don’t see the brick in the Game view, make sure that the brick’s Scale is set to 1, 1, 1 (sometimes when creating a new Orthello object, the X and Y scale are set to a very small number).

Adding Collision To The Brick:

We’re going to need some collision on the brick later on so that the player knows when he/she’s standing on the ground.

  • With the brick object still selected in the Hierarchy, check the Collidable checkbox. This will automatically add a Box Collider and Rigidbody component to the sprite.
  • We also need to Tag the object with a specific tag and add it to a Layer. Go to Edit –> Project Settings –> Tags to open the Tag Manager.
  • Under Tags at the very top, drop down the little arrow and then type “Ground” (without the quotes) into the Element0 field and press enter. We’re going to need a few more tags later on so while we’re here, add another tag called “Ladder” and “Rope“.
  • We need some Layers too so under User Layer 8, type Ground and under User Layer 9 type Ladder.
  • Click on the brick object in Hierarchy and the drop down the Tag list in the Inspector and select Ground. Then click on the Layer drop down and select Ground from that list.

Turn It Into A Prefab:

Later on in the tutorial series we’re going to be adding some stuff to the brick object and making some changes so it’s a good idea to turn the object into a Prefab so that if you build a level with the brick and then and then want to make changes to it later, the changes will be applied all the prefab brick objects in your level. Making a prefab is super easy and will save you a ton of time later on.

  • Create a new folder in your Project and name it “Prefabs“.
  • Drag the brick object from the Hierarchy and drop it into the Prefabs folder in the Project tab.

Making The Static Level Sprites

Next we need to make the concrete, ladder and rope tiles. We’re going to use the same Container for these that we made before but instead of displaying them with an AnimatedSprite, we’re going to use the Sprite object.

Concrete Tile:

  • If your brick object is still sitting in the center of the Scene, move it to the side so it’s out of the way.
  • Drag & drop the Sprite object from Orthello –> Objects –> Sprites into the Hierarchy or Scene which will create a new object named something like “Sprite (id=-3700)“. Rename that object to “concrete“.
  • Drag the level object that we created earlier from OT –> Containers and drop it on to the Sprite Container slot in the Inspector.
  • Your sprite will appear but it looks like the brick sprite that we made before, that’s because the brick is the first texture on the Sprite Atlases index. Click and hold your mouse over the word “Frame Index” in the inspector (with the concrete object selected) and then drag the mouse to the right to scroll through the textures on the sprite atlas. The concrete texture is at index 14 so set it to that. The sprite should now look like a solid brick.
  • Check the Collidable checkbox to add collision to the object.
  • Drop down the Tag list in the Inspector and select Ground. Then click on the Layer drop down and select Ground from that list.
  • Drag the concrete object from the Hierarchy into the Prefabs folder in the Project tab to create prefab from the object.

Ladder and Rope Tiles:

The Ladder and Rope tiles follow most of the same steps as the Concrete tile.

  • Move concrete tile out of the way if it’s still sitting in the center or the Scene view.
  • Drag & drop a Sprite object from Orthello –> Objects –> Sprites into the Hierarchy or Scene and rename the object to “ladder“.
  • Drag the level object that we created earlier from OT –> Containers and drop it on to the Sprite Container slot in the Inspector.
  • Change the Frame Index to 15, the sprite in the Scene view should now look like a ladder segment. That’s all we need to do on the ladder tile for now.
  • To make the Rope tile, duplicate the ladder sprite in the Hierarchy and then rename it “rope“.
  • Change the Frame Index to 17 so that it looks like a black cube with a white line across the top (that’s our rope tile!).
  • Now make prefabs from the ladder and rope by dragging each of the objects from the Hierarchy into the Prefabs folder in the Project tab.

Making The Bottom Border:

Now we have all the sprites necessary for making levels, but before we start we need to make a border object that will sit at the bottom of the screen. The border will give the player and enemies something to stand on if there are missing bricks along the bottom row and it will also give us something to snap to so that the levels will adhere to a nice grid.

  • Go to Game Object –> Create Other –> Cube and rename it to “border bottom
  • Change the Transform Position to X: 0, Y: -10.3, Z: 0
  • Change the Transform Scale to X: 26, Y: 1, Z: 1
  • Drag and drop the border bottom object from the Hierarchy into the Prefabs folder in the Project tab to turn it into a prefab.

A small amount of the cube should be visible above the bottom of the Game view and it shouldn’t quite be all the way to each edge. The cube is pretty ugly with the default white material, so let’s make a material that matches the color of the bricks.

  • Create a new folder in the Project tab and name it Materials.
  • Right click on the Materials folder and go to Create –> Material and then rename the new material “border
  • Select the border material and then in the Inspector click on the white area beside the little eye dropper icon, this will open up the color picker.
  • Change the RGBA settings to R: 159, G: 2, B: 0, A: 255 and then close the color picker.
  • Drag the border material and drop it on to the border bottom object in the Hierarchy to apply the material. The border should now be the same color as the bricks.
  • With the “border bottom” object still selected, click on Apply next to Prefab in the Inspector, this will update the prefab with the new changes.

Change The Background Color

We’re almost ready to make a level, but first lets change the background color to black.

  • Select the Main Camera and then click on the color swatch next to Background.
  • Change the RGBA settings to 0,0,0,255. The background in the Game window should now be black.

Making A Level:

Whew! It took a few steps, but now you have everything you need to build a bunch of levels. Well almost everything – you still need a player, enemies, a pickup and a few other things which we’re going to cover later on in the series.

At this point you can just start duplicating the brick, concrete, ladder and rope tiles around the scene to make a level. BUT before you do, here are a few tips:

  • In the Scene view, click on the drop down list directly under the Scene tab – it might say something like “Textured” and change the option to Tex-Wire.
  • You can use Vertex Snap to easily align objects – in the Scene view hold down the V key and hover the mouse over any of the 4 corners on one of your tiles and you’ll see that the manipulator will snap to the closest corner. Click the left mouse button when the handle is over a corner of the sprite that you want to snap from and then drag the object to another sprite corner to align it tightly with that tile.
  • Remember that bottom border cube we made before, well you can use that as a base line for snapping your cubes so that you can build the entire level on a grid. Grab one of your tiles and snap the lower left corner to the upper corner of the bottom border object. Note that you don’t have to worry about snapping on the Z axis since Orthello doesn’t allow you to move objects along the Z axis.
  • You can also select several tiles at the same time either with shift+left click or by dragging an area around a bunch of tiles. Duplicate those tiles and then use Vertex Snap to snap them to other tiles in the scene.

Here’s what my initial scene looks like as I start by building the level from the bottom border object (click to see a larger version):

And here’s what a finished level looks like (click to see a larger version):

Conclusion:

I hope you enjoyed this post and learned a little more about how to make 2D games with Unity. In the next post we’re going to be adding a playable character so you can actually run around in the levels.

If you like this post, please be sure to say hi in the comments and follow me on Twitter and Facebook. Your support helps to keep these tutorial coming. This blog post is part of iDevBlogADay, a collection of indie developers writing about their development experiences.

Download the project up to this point.

More Tutorials In This Series

Make A 2D Game in Unity3D Using Only Free Tools Part 1
Make A 2D Game in Unity3D Using Only Free Tools Part 2
Make A 2D Game With Unity3D Using Only Free Tools Part 3
Make A 2D Game With Unity3D Using Only Free Tools Part 4
Make A 2D Game With Unity3D Using Only Free Tools: Beginning Enemy AI With A* Pathfinding

And here’s another tutorial series that uses Sprite Manager 2 for the sprite display and animation duties:
Creating 2D Games with Unity3D Part 1
Creating 2D Games with Unity3D Part 2
Creating 2D Games with Unity3D Part 3
Creating 2D Games with Unity3D Part 5

Show Your Support

If you find these tutorials useful, please considering buying one or more of our games and apps for iPhone and iPad. Your support helps to keep the tutorials flowing!

  • Giant Moto: High flying arcade style motocross action!
  • Small Space: Fly a spaceship, eat space creatures, score point!
  • Holeshot Drag Racing: High speed drag racing action!
  • Alien Booth: Transform pictures of your friends into awesome aliens!
  • iSpoof Walken: A hilarious look into the world of Christopher Walken!
  • Gaga Eyes: Transform pictures of your friends to give them huge anime eyes!

About the author

Tim MillerTim Miller is a veteran game developer with over 15 years experience in the video game industry working as a Level Designer and Lead Level Designer at LucasArts, Secret Level and Nihilistic Software. He co-founded Rocket 5 Studios with his wife Cathy and their dog Rocket in 2009. Twitter, LinkedIn, Portfolio

Make A 2D Game in Unity3D Using Only Free Tools Part 1

Make A 2D Game in Unity3D Using Only Free ToolsIn this iDevBlogADay post, I’m going to kick off a new tutorial series that’s aimed at making a 2D sprite-based game in Unity3D using only freely available tools, scripts and plugins. This isn’t the first 2D in Unity3D tutorial series I’ve done here – I previously did a 5 part series that used Sprite Manager 2 for the sprite display and animation duties. This time around, I want to show you how to make a game basically for free and I’m going to up the ante in this new series by adding in some other great plugins like iTween and A* Pathfinding.

Over the course of this series, we’re going to recreate one of my all time favorite C64 games: Lode Runner! I always wanted remake Lode Runner for my own amusement but also as a way to try out some things I haven’t figured out how to do yet – like creating an AI that can follow the player on ladders.

In this first installment, I’m going to introduce you to the tools we’ll be using and show you how to set them up in Unity. In the next part, we’ll dive in and start making the game. While we’ll be using the free versions of these tools, most of them also have paid options which unlock additional features.

Tools:

  • Unity3D: You probably already know that Unity3D is great tool for making 3D games, but it’s also pretty great at making 2D games with the addition of a few scripts and plugins. We’re going to be using the free version of Unity which will allow you to publish games to web as well as PC and Mac standalone but you can easily make the game work on iPhone or Android with a paid upgrade to those versions.
  • Orthello 2D Framework: There are several different sprite plugins available for Unity, the most popular being Sprite Manager 2 which I have used extensively and wrote about in my earlier 2D game tutorial series. I recently found myself looking for an alternative to SM2 and while doing research, @jeedee mentioned that he was quite satisfied with Orthello so I thought I’d try it out. In some ways it’s not as easy to use as some of the other plugins out there – you have to make your own sprite atlases for example – but for a free plugin it offers a ton of great features and will work perfectly for our 2D project.
  • iTween: iTween is my go-to animation system for every one of my projects here at Rocket 5. It’s ideal for animating everything from enemies to UI and it’s usually the first script I install when starting a new project.
  • A* Pathfinding Project: A* Pathfinding is probably the most widely used pathfinding system available for Unity. It’s fast, powerful, easy to use and since there’s a free version it’s perfect for our project.
  • TexturePacker: TexturePacker is a standalone app that makes it easy to create sprite sheets from your textures. You could use an image editing app like PhotoShop, Acorn or Gimp to make your atlases, but Orthello 2D recently added direct support for atlases generated by TexturePacker which should make things easier for us later on.

Starting A New Project:

  1. Install the latest version of Unity and then create a new project by going to File -> New Project, click the Set… button and browse to a location on your drive where you want to save your Unity projects, enter a name for the project and then click Save. Optionally you can select any packages that you want to import into the new project, I didn’t import any packages into my new project. Finally click the Create Project button.
  2. Create a new folder in your Project view and name it “Scenes” and then save the current scene by going to File -> Save Scene As, open the Scenes folder you created in the previous step, name it “level1” and then click Save.

Installing Orthello 2D:

  1. Open the Asset Store by going to Window -> Asset Store, search for “orthello2d“. The page for the plugin should display, click the Download button. Or download the latest version from the website, unzip the archive and double click on orthello.unitypackage.
  2. When the Import Package window appears, make sure that all the checkboxes are checked and then click Import. After a few seconds you should see an “Orthello” folder in your Project view.

Orthello 2D Initial Setup:

The Orthello website has tons of detailed information on setting up and working with the plugin so be sure to take a look. Following are my simplified steps to get you started.

In order for Orthello to work in the scene, you’ll need to setup a few things first. Note that you will need to repeat these steps in every new scene you create.

  1. In the Unity Project view, go to Orthello -> Objects and then drag the OT prefab into either the Scene view or the Hierarchy.

The OT prefab acts as a parent for Animations and Sprite Containers that we will be adding to the scene later on. Adding the OT object to your scene will also automatically make some changes to the Main Camara so that it will work properly for a 2D game – the main things are it changes the Projection to Orthographic and sets the Size to 332 (332 is kind of a weird size, but that’s the size that Orthello likes).

Installing iTween:

  1. Open the Asset Store again by going to Window -> Asset Store, search for “itween“. The page for the plugin should display, click the Download button.
  2. When the Import Package window appears, make sure that all the checkboxes are checked and then click Import. After a few seconds you should see an “iTween” folder in your Project view. If you want you can delete the “ReadMe!” and “Sample” folders if you want but they won’t hurt anything and it’s a good idea to look at the sample scene if you’ve never used iTween before.

Installing A* Pathfinding:

As I write this, the free version of A* Pathfinding is not available for download from the Unity Asset Store (the paid version is though).

  1. Go to the website, click on Download Latest Version and then click on A* Pathfinding Project in the Download area which should download a file named PathfindingProject_Free.unitypackage onto your computer.
  2. Double click on PathfindingProject_Free.unitypackage which will open the Importing Package window in Unity. Make sure all of the checkboxes are checked and then click Import. You should now see a folder named “AstarPathfindingProject” in your Project view.

A* Pathfinding Initial Setup:

Be sure to read the “getting started” page in the A* Pathfinding Documentation for a full explanation of how to setup A*. Here are my simplified steps to get you going.

  1. Create a new empty game object by going to GameObject -> Create Empty.
  2. Make sure that the x,y,z position of the game object is at zero and then rename the object to “A*“.
  3. Add the Astar Path script to the game object by going to Component -> Pathfinding and then click on Pathfinder.
  4. With the A* object selected in the Hierarchy, you should see Astar Path script’s options in the Inspector. At the top of the script you should see a message that says “Do you want to enable Javascript support?”. Since all of the scripts used in this tutorial are going to be written in C#, you can go ahead and click “No” (you can change this later in the settings if you want).

Conclusion:

If you followed all of the steps above, then your project should look something like this (click to see larger image).

The cool thing about the project is that you can use this as a starting point for creating all kinds of 2D games so keep this around as a base for starting new 2D projects (although be sure to check for the latest versions of the scripts).

More Tutorials In This Series

Make A 2D Game in Unity3D Using Only Free Tools Part 1
Make A 2D Game in Unity3D Using Only Free Tools Part 2
Make A 2D Game With Unity3D Using Only Free Tools Part 3
Make A 2D Game With Unity3D Using Only Free Tools Part 4
Make A 2D Game With Unity3D Using Only Free Tools: Beginning Enemy AI With A* Pathfinding

And here’s another tutorial series that uses Sprite Manager 2 for the sprite display and animation duties:
Creating 2D Games with Unity3D Part 1
Creating 2D Games with Unity3D Part 2
Creating 2D Games with Unity3D Part 3
Creating 2D Games with Unity3D Part 5

Show Your Support

If you find these tutorials useful, please considering buying one or more of our games and apps for iPhone and iPad. Your support helps to keep the tutorials flowing!

  • Giant Moto: High flying arcade style motocross action!
  • Small Space: Fly a spaceship, eat space creatures, score point!
  • Holeshot Drag Racing: High speed drag racing action!
  • Alien Booth: Transform pictures of your friends into awesome aliens!
  • iSpoof Walken: A hilarious look into the world of Christopher Walken!
  • Gaga Eyes: Transform pictures of your friends to give them huge anime eyes!

About the author

Tim MillerTim Miller is a veteran game developer with over 15 years experience in the video game industry working as a Level Designer and Lead Level Designer at LucasArts, Secret Level and Nihilistic Software. He co-founded Rocket 5 Studios with his wife Cathy and their dog Rocket in 2009. Twitter, LinkedIn, Portfolio

Creating 2D Games with Unity3D Part 5 Complex Collision

collision iconWay back in the first part of this series, I mentioned that you can make complex collision objects in 3D packages like Maya or Blender to match the shape of your 2D sprites. Unity3D provides basic primitives like cubes and spheres, but sometimes you need more complex shapes to match the contours of the objects in your scene.

Even though this post is part of the “Creating 2D Games” series, these techniques will work for 3D games too. In fact, I created all of the 3D models for my iOS game Giant Moto using many of these techniques. I’ll be using Maya 2008 in this tutorial, but these concepts can be applied to your favorite 3D modeling package.

More Tutorials In This Series

Rocket’s Game, Example 1

This example shows how to make collision objects to match the shape of your 2D game characters. When a bullet collides with the Cat’s space ship, a small sphere will appear briefly to show where the bullet hit the ship’s collision mesh.

Click the image to load the example game in a new window:
Rocket's Game Collision Example

Making Collision For Rocket’s Ship

You can download “RocketsGame” project folder here. Remember that in order for the sprites in my project to display properly, you’ll need to use the Sprite Manager 2 plugin which is not included in my project. Once you have the project open in Unity, double click your “SM2 1.84.unitypackage” to import the package into the project. After that, you might also need to reassign the Sprite Material to the sprites in the scene – refer to Creating 2D Games with Unity3D Part 2 for details on how to do that. Note that I’m using Sprite Manager 2 v1.84.

After your project is set up and your sprites all look correct, we need to take a screenshot of the scene so that we can create a 3D model in Maya that will match the sprites exact shape. Use the following script to take a screenshot of your game (this is also included in controls.js in the project files).

function Update () {
	if(Input.GetKeyDown("s")) {
		print("screenshot");
		Application.CaptureScreenshot("Screenshot.png");
	}
}
  1. To take a screenshot, press the play button in Unity and then press the “s” key on your keyboard (assuming you’re using the script above). The word “screenshot” should print to the console to confirm it worked and you should find Screenshot.png in your project directory. Here’s what mine looked like.
  2. Open up Maya and confirm the following settings. Go to Window –> Settings and Preferences –> Preferences and then click on Settings. Make sure the “Up axis” is set to “Y” and Linear Working Units is set to “Centimeters”.
  3. To confirm that everything is working between Maya and Unity, create a 1x1x1 cube at 0,0,0 in Maya and then save the file in your project eg. RocketsGame/Assets/Models/cube.ma. Then switch back to Unity and drag the “cube” object into your scene. While still in Unity, go to Game Object –> Create Other –> Cube to create a cube. The cube you created in Maya and the one you created in Unity should be exactly the same size.
  4. Next we need to find the Unity/Maya unit size of the Screenshot.png file you made in step 1. Scale up the Unity cube you made in step 2 so that it fills the Game view exactly. My example project is set to 480×320 so my cube fills the screen at Scale X:15, Scale Y:10. Since we confirmed in step 2 that our Maya to Unity scale is 1:1, now we know how big to make the image plane in the next step.
  5. In Maya, create a plane Create –> Polygon Primitives –> Plane and then drag a plane out on the grid. Set the Translate X, Y and Z to 0 and set Rotate X to 90. Under polyPlane1’s INPUTS, set Width to 15 and Height to 10 (assuming your project is 480×320 like mine).
  6. With the plane selected in Maya, right click + hold on the plane and then go to Assign New Material –> Lambert. In the Attributes editor, click on the the little black/white checkerboard icon next to Color to bring up the Create Render Node window and then click on File. Now click on the the little folder icon next to the Image Name filed and browse to the location of the Screenshot.png we created in step 1 and click ok. You should see the .png on your plane, if you don’t press 6 on the keyboard to switch to texture view.
  7. Go to Create UV’s –> Planar Mapping and adjust the Projection Attributes in the Attributes Editor until the image looks correct on the plane. Click here to see what mine looks like.
  8. Now we’re finally ready to create the collision to match the sprite. Switch to the Front camera so that you’re looking at an orthographic view of the image. Go to Mesh –> Create Polygon Tool and trace out the shape of the sprite. Once you have the shape you like, you need to extrude the object to give it depth so that objects in your scene will be able to collide with it in 3D space. Go to Edit Mesh –> Extrude and extrude the mesh on the Z axis only so that it has about 1 unit of depth.
  9. You also need to make sure that the normals on the collision are facing out. With the object selected, open the Attribute Editor and then under Render Stats, make sure that “Double Sided” is unchecked. Now you should be able to see the actual polygon faces on your object. If any faces are pointing inward, you need to select those faces and then go to Normals –> Reverse.
  10. Center the pivot on your mesh and then snap the object to 0,0,0 on the grid to make sure that it’s perfectly centered in the scene and then Freeze Transforms to zero out the transforms.
  11. Now you could just save the scene into your project folder to import it into Unity. But the scene also has a plane with Screenshot.png on it which we don’t want in our scene – our sprites are being drawn with Sprite Manager so we don’t want this textured model in there too. Select the object in Maya and then go to File –> Export Selection, then click Export Selection and browse to the location in your Unity project where you want to save the model.
  12. Switch back to Unity and drag the collision object (“rocket” in my case) from the Project view into the Scene view. Set the Transform X, Y and Z to zero and you might also need to rotate the object 180 on the Y – I think the camera in Maya is on the opposite side as the camera in Unity so you can end up with mirrored results if everything isn’t setup properly.
  13. One more thing, we need to generate colliders on the mesh. With the object selected in the Project view in Unity, you should see the FBXImporter settings in the Inspector. Click the Generate Colliders checkbox and then click Apply.

Here’s what my finished collision looks like in Unity (I put a transparent material on the model so you can see the sprite through it).

Collision object in Unity

Whew. That looks like a lot of work, but once you do it a few times it’s a pretty quick process. As always, if anyone knows of a quicker way to do this please let us know in the comments.

What you have now is a collision object that’s very much like a stock Unity cube or sphere Game Object which means you can treat it like any other object in your scene. I’m not going to go into detail on how to make this new collision object into a playable character, but basically you just need to add your player sprite object as a child of the collision so that the sprite will move with the collision, uncheck the Mesh Renderer on the collision so it’s not visible and then add your control script to it. Again you can download the project here to see exactly how I have my scene setup.

Pachinko Thing, Example 2

Here’s another example that applies the techniques from above to static and animated level geometry. You can download the “2DPachinko” example here (again, you need to provide your own Sprite Manager plugin).

Click the image to load the example game in a new window:
Pachinko Thing collision example

And here’s what the collision looks like in Maya and in Unity.

Maya and Unity collision

Conclusion

This post demonstrated a couple of the possibilities when combining 2D art with 3D collision objects in Unity3D. I’m always looking for ways to improve my own workflow, so if you see any errors or have any suggestions, please feel free to post in the comments. Oh and the little kitty cat that’s flying the red rocket is from the Open Clip Art Library.

This post is part of iDevBlogADay, a group of indie iPhone development blogs featuring two posts per day. You can keep up with iDevBlogADay through the web site, RSS feed, or Twitter.

More Tutorials In This Series

Creating 2D Games with Unity3D Part 1
Creating 2D Games with Unity3D Part 2
Creating 2D Games with Unity3D Part 3
Creating 2D Games with Unity3D Part 4
Creating 2D Games with Unity3D Part 5

And here’s another more recent tutorial series that uses Orthello (free) for the sprite display and animation duties:
Make A 2D Game in Unity3D Using Only Free Tools Part 1
Make A 2D Game in Unity3D Using Only Free Tools Part 2
Make A 2D Game With Unity3D Using Only Free Tools Part 3
Make A 2D Game With Unity3D Using Only Free Tools Part 4
Make A 2D Game With Unity3D Using Only Free Tools: Beginning Enemy AI With A* Pathfinding

Show Your Support

If you find these tutorials useful, please considering buying one or more of our games and apps for iPhone and iPad. Your support helps to keep the tutorials flowing!

  • Giant Moto: High flying arcade style motocross action!
  • Small Space: Fly a spaceship, eat space creatures, score point!
  • Holeshot Drag Racing: High speed drag racing action!
  • Alien Booth: Transform pictures of your friends into awesome aliens!
  • iSpoof Walken: A hilarious look into the world of Christopher Walken!
  • Gaga Eyes: Transform pictures of your friends to give them huge anime eyes!

About the author

Tim MillerTim Miller is a veteran game developer with over 15 years experience in the video game industry working as a Level Designer and Lead Level Designer at LucasArts, Secret Level and Nihilistic Software. He co-founded Rocket 5 Studios with his wife Cathy and their dog Rocket in 2009. Twitter, LinkedIn, Portfolio