Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple 2D game example

Discussion in 'Editor & General Support' started by bobmn, Dec 5, 2007.

  1. bobmn

    bobmn

    Joined:
    Dec 4, 2007
    Posts:
    7
    Hello all,

    I'm using the trial of Unity and think it's really cool. I plan on buying it if I can put together a proof of concept of a simple 2D game. What I can't wrap my mind around is how to put together a simple 2D type game. I have searched google and these threads for a simple example of a 2D game Unity with little to no luck. I found a few hits in the forum on 2D say like 1 or 2 posts. But there was nothing concrete in them for a newbie like myself.

    What I'm looking for is is to do a game with graphics and environment similar to ->http://www.ambrosiasw.com/games/sketchfighter/

    That is to say I want to do a top down 2D game.
    How do I put a 2D map down?
    How do I put 2D images for all the characters on the map?

    Can someone put a demo with source out there for us trying to do a 2D game in Unity. If it's so easy I would think a person could whip one up over the course of a few days with minimal time. I know it can be done but when learning something it really helps to have a concrete example to learn by. I would love to get my hands on the source and make a video tutorial for others to watch and learn. Unity has some good 3D perspective ones, but no 2D.

    Please keep in mind I'm new to Unity and to game programming though I have been programming database business apps for 8 years.

    Thanks for any help it's appreciated

    Bob
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Does this help? (The project file is the third one; should have labeled them, sorry....)

    As for a 2D overhead map, you could use GUITextures, or a polygonal plane. Same for the characters. There's really no functional difference from 3D, except you'd just use an orthogonal camera (probably).

    --Eric
     
  3. bobmn

    bobmn

    Joined:
    Dec 4, 2007
    Posts:
    7
    Thanks Eric!

    Not sure why my search on the forums didn't pull this back. Maybe I just missed it.

    I'm at my day job now and don't have access to Unity or my Mac. But this looks like just what I was hoping for. This is appreciated very much. I plan on digging into over the next couple of days and learning all I can from it.

    You folks have a great community.

    Again thanks
    Bob
     
  4. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This might be a better example than the Unity Invaders thing. It's really basic, and not even a complete game. (No scoring, goals, lives, etc., which I figure has been covered well enough elsewhere.) Just 100% pure 2D, no fancy stuff. ;) This way there isn't much code, but what is there is still completely functional, hopefully....

    It builds a level from a text file: "X" = wall, "o" = turret, "." = save point, "1" = player's ship. It figures where the straight edges, corners, etc. should be, so you just use "X" for a wall and it will do the rest. The level can be any arbitrary dimensions...20x20 (like the one included), 33x5, whatever. There's some basic error checking, so it's OK to do stuff like hit return at the end of the last line (any lines not the same length as the first are ignored), or include a bunch of "1"s in an attempt to make multiple player ships (only the first is used).

    Probably it would be best if the level were built into a combined mesh (or at least used the mesh combine utility), but that's somewhat more complicated for just a basic example like this. Right now each tile is a separate object. Still, it gets ~460fps on my G5, so it's probably not that big a deal in this case. ;) If you had really huge levels or more detail, like smaller tiles, it would become more important.

    It uses raycasting to check for wall collisions; this way the tile graphics can be shaped like anything and the collisions are still pretty accurate. If you want, you can add additional raycasting points to the ship for more accurate wall collisions. Right now there are just three, one at each corner, which seemed OK most of the time. To add more, drag the Ship prefab into the scene view or hierarchy, making sure you have a top view with orthographic view on, and hit F to focus on it. Duplicate one of the little RaycastPoint spheres that are children of the ship (it will lose the prefab connection, which is OK), and position it where you want. Repeat as desired, then select the parent ship and do GameObject -> Apply Changes To Prefab from the menu, and get rid of the ship object in the hierarchy. The spheres obviously get turned off when the ship is instantiated, and are just there to provide a visual reference. More points = more raycasting = slower, but then again, at 460fps, I probably could have added some more without much impact....

    You can also fiddle with various parameters on the Ship prefab (speed, bullet speed, etc.), and on the TurretTrigger prefab (rotate speed, repeat rate, etc.). Plus a lot more, of course, if you want to mess with the scripts. Default controls = left/right arrows to rotate ship, up arrow to move forward, space to fire, R to reset. Have fun. ;)

    Edit: there's an updated project below...the only reason to look at this one is if you want to see the text file -> map code for some reason.

    --Eric
     

    Attached Files:

  6. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    That looks really cool Eric, I look forward to checking it out in future.

    AC
     
  7. bobmn

    bobmn

    Joined:
    Dec 4, 2007
    Posts:
    7
    Wow thanks for posting this! I started looking at the space invaders clone you posted before and I figured some stuff out, but it was a little much for a complete newbie to game programming and Unity. This example appears to be just perfect.

    Are you Santa?

    Appreciated!

    Bob
     
  8. bobmn

    bobmn

    Joined:
    Dec 4, 2007
    Posts:
    7
    So besides raycasting is there any other easy and good way to do accurate collision detection in 2D?
     
  9. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Holy sweet Hannah, Eric! While the one set of code that takes the XOXO text object and converts it to a map is way (way) beyond me right now, I certainly appreciate the stunning amount of work that went into that and it's in a cherished place on my hard drive.

    Wow.

    And yes, Widget United is nearly there ... I'm almost at variable tweaking stage. News soon.
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not that I can think of. One thing I tried before was reading a pixel's color off the screen instead of raycasting. This has the advantage of being able to use compressed textures, but too many disadvantages: can't selectively ignore objects, can't do a simple alpha test like in this code (would have to rely on color values), and it's quite a bit slower. While Unity is flexible enough to do a pretty good job with 2D, it's not really a 2D engine, and you can't really do screen blits and stuff in an efficient way. At least not that I know of.

    As long as you have geometric shapes, you can always use primitive colliders, like with the turrets and bullets. You could also build a mesh collider that roughly matches the tile shapes, but that might be tough to automate. I guess the easiest way to do that would be to make some mesh tiles that correspond to the graphic tiles, and build a combined mesh using the mesh scripting interface.

    @DaveyJJ: thanks, but it wasn't actually a big deal; one advantage of having already programmed a decent amount of Unity stuff is that I was able to rip off code from other projects to speed things up a lot. :) Definitely looking forward to seeing Widget United, BTW.

    --Eric
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I've been working on a level editor for another project, so I adapted it for the 2D shooter example. This might make Davey "Give me level editors or give me death!" JJ happy. ;) It's really basic, but it should give some idea as to what's involved in making a map editor. I also fixed a few stupidities I found in the project; I should probably delete the first example, but the "build a map from a text file" thing might still be of some interest, maybe. Apologies for any stupidities that remain....

    This removed most of the complexity from the CreateLevel script and put it in the level editor script, which is a lot more like real games. So now the level format directly represents the tiles that actually make up the level.

    What you can do in the editor is pick from four tiles, and place them in the map with the mouse. It's easy to add more objects; probably I could do a simple tutorial for that. Left click places, right click or control-click erases. You can only put in one instance of the player's ship. The wall tile is actually a collection of 8 tiles: like the Starcraft and Warcraft III editors (and others), you just draw it in, and it adapts the tile and the tiles around it as necessary. Also similar to the aforementioned editors, there are a couple of alternate tiles, so drawing straight edges, for example, randomly picks from two tiles as you go, which helps break up repeating textures a bit.

    You can scroll around by the screen edges, or the keyboard (arrow keys or WSAD), or by middle-clicking/holding down option and dragging. Mouse wheel zooms in/out, or you can use [ ] or home/end. All the keys can be set in the input manager, of course. In the Levels pane, Clear does just that, and you can make levels of arbitrary size with the text fields. When you hit Set Size, it resets the map to the dimensions you entered, which also clears it.

    Saving just makes a file called "level01.txt", which is in the root folder of the project if you run from the editor, or next to the app if you build a stand-alone. If you want to use it in the Game level, copy it to the TextAsset folder. Using files as TextAssets means they get included with your game when you build it, so you don't need external level files. Of course, maybe you'd want that (for user-created levels, for example), so in that case you'd just use .net routines instead of TextAsset, like the level editor script does. Using external files also means you could use a binary format for the levels instead of text.

    Loading just loads the "level01.txt" file. If you're detecting a distinct lack of file requesters here, you're correct. ;) It's actually not hard to make them with the new GUI routines, though. Like I said, pretty basic. There's a lot that could be added, like undo, or limiting the width of the tiles pane and having it scroll if there are a bunch of tiles, filling in areas with the selected tile, etc.

    --Eric
     

    Attached Files:

  12. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Just tried out the first example. Nice one and old skool! I guesss the player ship needs one further point between each edge as otherwise collisions sometimes get lost.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep, I added a few more points in the second example. I also made the raycast check a separate script, so you can attach it to any object, and put some raycast points on it like is done with the ship, and it'll work too. As long as the object has another script with a function called Explode.

    --Eric
     
  14. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Okay will check this out. I instantly had to think of the puzzle part in IM... ;O)
     
  15. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    G'day Eric,

    Thanks for this very generous share. It's a great example and has lots of little tidbits in there.

    Cheers.
     
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're welcome, and thanks for the comments. I've just updated it, with a few minor changes (in particular, the RaycastCheck script wasn't what it should have been).

    Perhaps I need a 1KHz digitized voice saying "Stay a while...stay forever!" ;)

    --Eric
     
  17. yker22

    yker22

    Joined:
    Jun 13, 2009
    Posts:
    6
    Wow,

    It seems that this is the sample I´m looking for to start learning Unity (to make a 2d game). But sadly I´m on a PC... I´m wondering if somebody knows about a place to get a sample project for Windows...

    Thanks in advance
    Isaac
     
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, that is sad ;), however project folders are the same on Mac + PC.

    --Eric
     
  19. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    Cool! Very nicely done. Quite complete.

    I see the forum post dates are from 2007 when this was made, so I'm wondering if there is anything more recent made?

    It ran fine the first time I did it, but then when I loaded the project again after working on something else, all I get is this error for both the game and the level editor.

    Code (csharp):
    1. Assets/2DShooter Assets/Scripts/FPSCounter.js(7,2): BCE0153: 'UnityEngine.RequireComponent' can be applied on one of these targets only : Class.
    I commented out the GUI bit, and it works fine.
    -

    I'd like to be able to use a 2D map editor to make a map quickly, and then load up in a 3D world. For the 3D world, all I'd have to do is load in the X and Y coordinates(saved in the 2D editor for the location of the 2D objects), and then the Z would just be 0 by default, keeping a nice flat even level. Or I think Unity does it with Y and Z backwards. Not sure. Anyway, loading up the name of what model to load, and the coordinates, is all it would take, correct? Or would something else be required? Looking through the scripts now to try to understand them.

    Has anyone else already done this?
     
  20. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This is a little more recent. The error is because of a mistake in the code, which Unity used to silently ignore. It should be "@script RequireComponent(GUIText)". I tried uploading a new version a while ago with this fix (so it would stop haunting me forever ;) ), but the forum doesn't want to accept larger attachments anymore, apparently.

    Yep.

    --Eric
     
  21. Pollyfun

    Pollyfun

    Joined:
    Nov 6, 2009
    Posts:
    13
    I'm just starting out to learn Unity, and this project would be a great help for understanding 2D. But the zip-file doesn't seem to work in Windows. A ton of error messages are created when I unzip.

    If someone could zip the project in Windows and either add it here or mail it to me, it would be greatly appreciated.

    thanks
     
  22. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Try using a different unzip app; the one built into Windows doesn't really work right.

    --Eric
     
  23. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I recommend to use 7 zip from http://www.7-zip.org
    its powerfull and allows you to compress lzma / lzma2 archives, which are more or less the smallest type of compressed archives you can have without forking out a fair amount of cash
     
  24. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I recommend to use 7 zip from http://www.7-zip.org
    its powerfull and allows you to compress lzma / lzma2 archives, which are more or less the smallest type of compressed archives you can have without forking out a fair amount of cash
     
  25. Pollyfun

    Pollyfun

    Joined:
    Nov 6, 2009
    Posts:
    13
    Thanks, that fixed it :)
     
  26. Orion TL

    Orion TL

    Joined:
    Sep 18, 2010
    Posts:
    1
    Hi i was wondering if you could upload the 2Dshooter game as a package since all the links glitch up when i open it or maybe give a tutorial showing

    (which scripts go on wich objects and which prefabs go where ETC...)

    :) thanks

    P.S. am using indie version and am 13 years old however i have a huge knoledge of game design and extremly basic javascrips skills :wink:
     
  27. TwistAU

    TwistAU

    Joined:
    Sep 28, 2010
    Posts:
    2
    This link worked for me. If you try to build it in Unity 3 you'll get a bunch of compile errors since it seems that they have made an official Unity class named 'EditorGUI' which conflicts with the script of the same name in the project. Just rename both the script file and all references to it in LevelEditor.js to something else like 'MyEditorGUI' and it'll work.
     
  28. Simba

    Simba

    Joined:
    Nov 8, 2010
    Posts:
    77
    Eric, thanks soooo much! I've been looking for a 2d level editor example for ages. :)

    Is there a way to save the scene as anything other than Level1.txt. Ie, how could I (and is it possible to) create a new text file? I've searched the scripting API, without success.

    Other than that, it's completely perfect.

    (Sorry for bumping an old thread. :rolleyes: )
     
  29. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Change the part in the script where it says Level01.txt to something else.

    --Eric
     
  30. Simba

    Simba

    Joined:
    Nov 8, 2010
    Posts:
    77
    Lol... I just found that out. I was looking in the wrong script.

    Is there any way to permanently change the Level.txt data?

    Also, if I changed the file name to a non-existent name, would Unity create a new file?

    Many thanks,

    Arthur