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

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?

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.

3 Likes

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.

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: 2D Sprite Shape | 2D SpriteShape | 10.0.6

1 Like

I was thinking of tile based. The tutorials iā€™ve found on tile maps seem to be about pre-painting them.

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.

Tile maps can absolutely be generated via code. It has a whole scripting API: Unity - Scripting API: Tilemap

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.

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.

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.

i think you need the advice of kurt decker

1 Like

Lol, Perth, youā€™re killinā€™ meā€¦ :slight_smile:

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:

1 Like

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.

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 Unity - Manual: 2D game development 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?

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.

2 Likes

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.

Ok! Back to more tutorials.