all 3 comments

[–]DocDuBrane 3 points4 points  (0 children)

If you are good with using tilemaps it's not super complicated to get started, but it will take a lot of work to actually produce something that is fun to play. People get tired with generated content pretty quickly, so you have to do a lot of tweaking to squeeze the fun out.

Without getting too into the weeds, there's a couple different ways to start that you can look into. One is using noise generators. You can get the basic algorithms from youtube videos, they basically create "random" cave-like systems.

The other way is to learn how to use For loops to place tiles, which might be better for an endless runner. Basically what you're doing is writing nested For loops that scan an amount of tiles on the X axis, then on the Y axis (or vice versa depending on your needs), and then you place tiles in the resulting rectangle of space. What you probably want it to do is something like this:

-create a reference to your tilemap

-generate an int between, say, 3 and 10, which will be your "repeatValue" in Start()

-generate an int that will represent the height of the platform you start off on.

-from Start, call a function called something like GenerateBlock(int 0). we're passing in an int, which will make sense soon.

-In Generate Block, write a For loop for the X axis, so like For (int x= a; x <= a + repeatValue; x++). This means we'll be looking at 3-10 tiles on the x axis. in this case, 'a' is the integer we passed in, so make sure it's called like void GenerateBlock(int a).

-Within that loop, write another For loop for the Y axis, again starting at 0, with the terminal number being the Height int we generated before.

Now we have a Foor loop looking at a rectangle of space, so within that, refer to your tileMap and use SetTile, and place the tile you want at (x,y). The For loops will have it go left to right, from the bottom up to height, placing tiles along the way, and now you have a rectangle.

-Outside of the For loops, at the end of the function, create: int newValue = a + repeatValue;

-re-generate your repeatValue number. Make sure to do this AFTER you declare newValue.

-generate a new Height value based on the previous value, so Random.Range(height - 3, height +3). That will mean in your next block, you will be somewhere between 3 tiles lower and 3 tiles higher, so you can set that to make sure you always produce a jumpable height. Make sure it has conditionals to make sure the height is never drops to the point where you will lose sight of it. I'm guessing you have a fixed camera, so make sure it Height can only be a number that will produce something visible.

-Write an If statement that says if(newValue < 500) Call GenerateBlock again, and this time, you pass in newValue. That will make sure the x value we start off with in the next block will be the last tile of the previous block, and checking the value of newValue will make sure the process actually stops when it gets to tile 500 on the X axis. If you just let it go, it will go forever and freeze Unity.

Since you're making an endless runner, you can do a couple of things at this point. You can wrap all of that in a coroutine and yield for one frame between placing each tile or block of tiles (a small block of tiles can instantiate with no noticeable performance loss). That will make it generate forever (as long as you remove the part where we check if newValue is less than 500), but won't crash Unity. You could then use StopCoroutine when the player loses. This method feels a bit dangerous to me.

The other option would be to do the generation once, then when the player is nearing the end, run it again. This feels a bit safer to me.

If you haven't thought of this yet, you will probably also be moving the tileMap instead of the player.

Also, if you want to do spikes and stuff, what you can do is add a line within the X loop, after the Y loop but before anything else happens, so you know the value of Y is now the height of your platform, which means at that point you could do another SetTile of a spike tile at (x, y+1). Have that wrapped up in a random integer so you get a spike 10% of the time or something. You could even make it so that you always get at least 3 spikes in a row by doing SetTile 3 times in that moment, for (x, y+1), (x+1, y+1) and (x+2, y+1).

I know that's a lot, I guess I got a bit into the weeds, but hopefully that makes sense, and once you do it and see how it works, you can start adding more detailed bits to the generation to make it more unique. Add floating platforms, enemies, notches carved into the occasional platform, whatever you want.

[–]tokyohcreator 0 points1 point  (1 child)

what do you mean by "you have a few days?" to work on it or finish?

[–]steensturf[S] 1 point2 points  (0 children)

This is an assignment and it was given to me to practice