Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Looking for advice to create a dynamic, right to left, scrolling platformer

Discussion in '2D' started by security_pedant, May 14, 2024.

  1. security_pedant

    security_pedant

    Joined:
    May 14, 2024
    Posts:
    7
    I am working with my sons to create our first Unity game. We have the idea all mapped out, and the first major challenge is dynamically creating the level.

    Our game is going to be similar in style to Jetpack Joyride, where a character starts on the left of the screen, and then progresses to the right. Our character will not fly, but walk along the ground and be able to jump and climb over oncoming obstacles.

    What I want to have in Unity, is an infinite (until the character dies that is) right to left scrolling map/level/scene, generated by code using a range of preset sprites/tiles.

    I've searched, and the only thing I can find that's close is this video that's 11 years old!



    It talks about using a mesh which is then dynamically generated as the player moves along.

    But the video is 11 years old... so i'm wondering if this is the current approach. I just need some pointers in the right direction. Unlike the video, I don't need to generate a smooth surface, my level will be built from a range of blocks. The player won't be able to move backwards, so the generation only needs to work from right to level, and the map can be forgotten as it passes through the camera.

    Anyone able to point me in the right direction?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,402
    A game like Jetpack Joyride would be using just a bunch of premade background elements that are usually designed to cleanly snap together, and are just instantiated ahead of the player and destroyed once they go past the player. You can apply the same principle with just pre-made (aka prefabs) sprites and sprite shapes to act as terrain obstacles.

    Usually in these games, the player is not moving, but everything is scrolling past them instead to produce the illusion of movement.

    There is more than likely more up to date tutorials on making infinite runner games.
     
    dstears, Kurt-Dekker and Spy-Master like this.
  3. security_pedant

    security_pedant

    Joined:
    May 14, 2024
    Posts:
    7
    I was using Jetpack Joyride as an example of the layout, but yes, unlike Jetpack, I don't want a set of premade scenes to scroll by, I want to actually generate the walkable terrain dynamically as it goes. Then I can add new sprites and biomes (aka Minecraft style) and just use code to render them out dynamically.
     
    Last edited: May 14, 2024
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,402
    Well when you say 'walkable terrain', what sort of style of terrain are we talking about? You mention Minecraft. Do you want the terrain to be tile based? In which case you'd use the tile map API and just generate tile ahead of the player.

    Or do you want something more naturally shaped (which includes both rounded and jagged shapes)? Then you'd probably want to use the Sprite Shape package/API: https://docs.unity3d.com/Packages/com.unity.2d.spriteshape@10.0/manual/index.html
     
    Spy-Master likes this.
  5. security_pedant

    security_pedant

    Joined:
    May 14, 2024
    Posts:
    7
    I was thinking of tile based. The tutorials i've found on tile maps seem to be about pre-painting them.

    https://learn.unity.com/tutorial/introduction-to-tilemaps#

    Are you saying I can also dynamically create tile maps and populate them via code?

    Rounded and jagged shapes you say? Sprite Shapes, now that looks interesting. I also assume this can be totally created in code? I.e. I don't have to draw the maps prior.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,402
    Tile maps can absolutely be generated via code. It has a whole scripting API: https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.html

    My own current project procedurally generates tile map environments. Though it's more accurate to say I build the pure data representation of the environment, then build a visual tile map representation from this data at runtime. Though that separation of concern might not be necessary for an infinite runner game.

    I haven't used sprite shapes via code, but pretty much all it's code is public judging via the scripting API in the docs I linked. I imagine some experimenting would lead to positive results.
     
  7. security_pedant

    security_pedant

    Joined:
    May 14, 2024
    Posts:
    7
    Actually that sounds like a great idea. I may want to do a range of processing as part of building the terrain as the game moves along. Like placing items, modifying them based on in-game current variables.

    So you are saying you create a 2D array of tiles, and any possible associated attributes, and then use that data to drive the function that builds the tilemap itself in game?

    I think now I have a direction (tilemaps API) I need to go play and build some sample ideas.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,402
    I'm not sure what the appropriate data structure is for an infinite runner. If you want to keep the 'infinite' part, then you will need to be generating things on the fly alongside releasing old data that you've passed.

    Perhaps a 2D (or 1D if the extra performance is needed) of a fixed size is still appropriate. Though as new data is generated, rather than pushing the data back an index and placing new data at the end, you have a 'pointer' that tells you where the end newest data is. As you add you data, you insert that just after the pointer, then move it up one position, and keep looping the pointer around the fixed array as necessary.

    Just theorising of course.
     
  9. pertholdth

    pertholdth

    Joined:
    Feb 28, 2024
    Posts:
    201
    i think you need the advice of kurt decker
     
    Kurt-Dekker likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,382
    Lol, Perth, you're killin' me... :)

    OP, get something going, anything going... even the simplest beginning is better than spinning around and around with indeterminism and uncertainty. As you get stuff going you will learn from it.

    The key is to decompose what you want into the teeniest tiniest little micro-step and then figure out that micro-step and DO it... then move onto the next piece.

    I like this guy's approach:

    Imphenzia: How Did I Learn To Make Games:

     
    pertholdth likes this.
  11. security_pedant

    security_pedant

    Joined:
    May 14, 2024
    Posts:
    7
    Yup... this is going to be my approach. But first, I wanted to get a feel for the right first path to take. And the answers above have helped me narrow down and also, make a few initial, critical game decisions. Thanks for the super fast responses everyone!

    Note i've been developing apps (Python, Javascript) in VS Code for many years. I'm just ramping up on Unity and C#.

    Half way through this video, it's absoltely perfect. It's taking me through the basics of game objects and how to attach components and code to them.
     
  12. security_pedant

    security_pedant

    Joined:
    May 14, 2024
    Posts:
    7
    Ok, i've spent a few days reading and could do with a few more pointers.

    I currently have a new 2D Core project open, with only a camera and a Grid > Tilemap. My goal is to build the basics of my 2D level, the layout is going to be something like the image below.

    When the game starts, I will generate box 1 and box 2 terrain, and the character can move to the right, revealing box 2. Box 1 remains in memory, and as box 2 moves into the camera view, i'll create a box 3 and so on. The character can move back fully into the previous box, but no more. So I have my main idea decided.

    What i'm struggling with is co-ordinates and the right way to structure the model. I've read through https://docs.unity3d.com/Manual/Unity2D.html and it seems to imply (and based on responses above) that I should be creating a Grid > Tilemap. I think I can instantiate the sprites into the Tilemap programatically.

    My questions are, to get me off the ground...

    Is box 1, 2 and 3 below each a tilemap? Inside which I can create all my sprites that represent the terrain? When I then introduce my character, other mobs and items, I use the camera view to get co-ordinates to place them? i.e. the camera defines the "space" where my character and other in game active objects live.

    If so, what are co-ordinates 0,0,0? Each gameObject seems to start at 0,0,0 (Apart from camera, which has z=-10). and 0,0,0 is in the middle of the object. Note that below my boxes are higher than the camera view, because in theory the terrain can go higher than the camera, which will need to scroll as the character goes higher.

    My first bit of code I want to write is, "Instantiate a row of terrain tile from the lower left start of box 1, through to the lower right. But if X0,Y0 is in the middle, what is the co-ordinate for the start of the lower left? Do I have to move the camera so that 0,0 of Tilemap 1 (box 1 in my image below) matches the lower left of the camera?

    upload_2024-5-18_11-6-42.png
     
  13. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,402
    Where did the image come from? Without context it's just an image with three boxes and some sprites. They could be tilemaps, but seeing as its without context we can't say.

    Are you talking about the position of a game object? That's just their position in 3d space. There's technically no differentiation between 2d and 3d projects, so all projects use 3d coordinates. But the tiles in tilemap have their own coordinates that are local to their respective tilemap. The tilemap itself has API methods to convert world positions

    If you're asking these kinds of questions I would say you may want to start with some guided learning first. Probably the junior programmer pathway: https://learn.unity.com/pathway/junior-programmer

    There's really no point in trying to proceed with your current project before you get to grips with the basics of Unity and programming. Something like working with tilemaps programmatically will expect you to be comfortable with general Unity stuff as it's fairly intermediate stuff.
     
    Ryiah and Kurt-Dekker like this.
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,382
    Again, you don't have enough grasp on what you're working with yet.

    When you write complex questions such as this:

    ... you have like 57 different compounding and confounding things going on, all interoperating with each other, causing you more confusion.

    As spiney suggests, work through guided learning tutorials that focus on one thing at a time.

    Do experiments as you go to confirm you are understanding what you are doing.

    There is no shortcut to "get you off the ground" any faster than you can learn.

    Really, there isn't. It's going to come down to your own diligent one-thing-at-a-time learning effort.
     
  15. security_pedant

    security_pedant

    Joined:
    May 14, 2024
    Posts:
    7
    Ok! Back to more tutorials.