Build a 2D Top Down Game - Zero to Published
I have copy/pasted this article from my website: www.IndieDevSpot.com I am cherry picking articles I like and dual posting them here.
Many of you know I have been waiting for the final stars to align so that I could write this article. I am pleased to announce that all issues technical and legal have been resolved, and I have been able to complete my game “Skeleton Dude”. The completed version of this game is published to the Windows 8 store and is available for download for FREE! This link will take you to the final version. So lets get started!
Part 1: Getting Started
Part 2: Animations
Part 3: Prefabs
Part 4: Enemies
Part 5: Modular Scripting
Part 6: Timed Prefab Instantiation
Part 7: Health Bars
Part 8: Finishing Touches
Part 9: Publishing
Download Unity for FREE!
One common misconception is that Unity costs money. That is only true if you want the PRO version. The free version is fantastic for pretty much everything an indie developer will want to do.
Create a new project
Open Unity, go to File -> New Project.
- Free version of Dungeon Kit.
- Unity VS if you are using it.
- Click here for all assets in a zip file if you are having issues. Please note that I received email permission from Icarus Studio to distribute this pack in this manner. Note that they have a paid for version that is way more awesome and is a great pack to download if you want to take this game to the next level.
Make sure you download the assets anyways, as it includes the splash screen as well as audio that I built using the Demo version of FL Studio.
Open your projects tab, right click on the assets folder and create new folder.
- Create a scripts folder
- Create Scenes folder
- Save Scene to Scenes Folder.
- Create Animations folder
- Prefabs folder
- Create an Audio Folder
Click on the skeleton image in your project tab and notice the inspector on the left:
Change the Texture type to Sprite, select Sprite Mode: Multiple. Click on Sprite Editor. Change the way it is slicing from Automatic to grid.
The reason we are choosing to do a grid in this instance is because these images are part of a sprite sheet. If the images were different sizes, when the animation were to play, it swaps out each sprite at a frame rate we define. If the sprites are different sizes, it will cause flashing, inaccurate collisions and other problems. Automatic is great for anything not being animated, otherwise try to use grid.
Create Spider Character
- From the top tab row: GameObject -> CreateEmpty
- Drag the first image of the spider sheet we created in the previous step onto the gameobject
- Rename gameobject to Spider
- Select the spider from the hierarchy tab, notice the inspector changes context to the spider. Select “Add Component”, select Physics 2D -> Add a circle collider. Adjust the size to be roughly the same size as your spider.
- Add a rigid body 2d (remove gravity) (in the same manner as step 4).
- Right Click in your Scripts folder in the project tab. Add two new C# scripts
- Drag both scripts into the component area of your spider in the inspect
Note that this image only has CharacterScript, it should also have CharacterInputScript.
Write Input Script
Require that the spider script be available and initialize the spider for control on start. Get Input, and make a call to SpiderScript to move the spider.
Double click the Character Input Script to open the script in Mono Develop or Visual Studio.
I have included touch support in the sample below. This is just a simple sample of how you can implement touch support for a top down game. For a more in depth touch support sample, please see the article located here. Even if you didn’t plan on adding touch support to your game, with most devices these days especially the windows 8 platform being touch devices, it is really something that you should do to increase your market and as you can see below is not too hard.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78||
Navigate to the input manager and add horizontal/vertical input
File->project settings ->Input
Remove everything except vertical and horizontal. Make sure it is mapped to your arrow keys. left, right, up, down.
Write the move function that allows the spider to move!
double click the CharacterScript from your project tab to open it in mono develop or visual studio.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32||
Write a function that allows the camera to follow the spider (taken from sample assets)
Create a new c# script in the scripts folder, name it Camera follow and drop the script on the camera. Also note, you can drop this same exact script on anything you want to follow the character, such as homing missiles.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47||
Add the spider as the target for the camera to follow. (I know this image says skeleton).
So we covered creating a new project, importing assets, creating sprite sheets, simple input, simple physics based movement, accessing components from script, modular scripting and initialization! Keep an eye out for Part 2, which will be out early next week!