Endless runner

Hi guys,

I’m looking at creating an endless runner style game and I’m not particularly sure about how I would go about spawning the platforms and making sure that they all connect to each other (end to end) so the player can continuously walk on them. I assume I would do this in an array but I’m really unsure of the code. Could someone point me in the right direction?

Regards,

It’s kind of like map generation, you spawn mesh actors or even mesh component of single actor when map or player (depending what you will move) moved in right distance. You could help yourself using sockets so meshes will be attached in right place

Hi, thanks for the reply. Sorry, I’m fairly new to this. I’m not quite sure how I would spawn the actor in a particular place in the scene. Can you possibly please show me an example?

So you probably wan’t something like the image below.
The blue box is your viewport size. The green line is a Y position at which you’ll want to spawn a new background.

Start by spawning the active background and the incoming background.
In tick, test to see if the active backgrounds B anchors Y position is less than the Y position of the green line. If it is then destroy the outgoing background, the active background becomes the new outgoing background, the incoming background becomes the active background and you spawn a new incoming background behind the active background.

Here’s some not real code that might be easier to understand.

Tick()
{
    //Scroll backgrounds left
    ActiveBG.Location.Y -= speed * DeltaTime;
    OutgoingBG.Location.Y -= speed * DeltaTime;
    IncomingBG.Location.Y -= speed * DeltaTime;

    //Test active backgrounds B location to see if it is going offscreen and requires a new background to be spawned.

    if(ActiveBG.BAnchor.Location.Y < GreenLine.Location.Y)
    {
         Destroy(OutgoingBG);
         OutGoingBG = ActiveBG;
         ActiveBG = IncomingBG;
         IncomingBG = Spawn(NewBackground);
         IncomingBG.Location = ActiveBG.Location + WidthOfABackground;
    }
}

You can also randomly choose the new background that your spawning each time to make things more interesting.
Hope this helps.

Note: This makes the background spawn scroll in a way that the player character should be kinda static in the level unless he collides with something.
A better way might be to have each background have its own green line and the test the characters position against the position of the green line in the active background to determine if a new background needs to be spawned. This way the background doesn’t need to be moving in the scene, just the characters lateral movement is necessary.

Well if you don’t know even that then maybe you should start by learning the basics, also you might want to learn how to do C++ to Blueprint interactions (it will be helpful in making mesh selection options) this tutorial will help you out it might also learn you some basics: UUqYfJeXqXHTzlZ2PeqPLi3A

As for spawning you do that using SpawnActor function:

you can get UWorld instance using (), so for example ()->SpawnActor(APlayerStart::StaticClass(),FVector(50,0,0)); will spawn Player start at postion X = 50

Hi ,

Thanks for that. I did try using the SpawnActor function in the way that you described above but I get this error:

error C2664: ‘T *UWorld::SpawnActor(UClass *,const FActorSpawnParameters &)’ : cannot convert argument 1 from ‘UClass *’ to ‘const FVector &’

Regards,

Ah there no overload liek that, try ()->SpawnActor(APlayerStart::StaticClass(),FVector(50,0,0),null,null);

Yeah, you’re right. I had to change it slightly to get it work though (it wasn’t happy with the nulls):

UWorld* const World = ();

if (World)
{
		World->SpawnActor<ALevelGen>(ALevelGen::StaticClass(), FVector(0, 0, -400), FRotator(0,0,0));
}

But I think I’m on the right path now. Thanks guys!

Thanks for taking the time to type out this reply! Unfortunately, I’m looking to do it in 3D but this will be helpful for something else I plan to do in the future! Thanks!

Every tile you create needs to have a box collision at the end that overlaps with player character. You also need to keep track of the last location that a tile needs to be spawned at. In the beginning of the game, you can spawn 5 tiles and then every time player character overlaps with the box collision you create another one at the last location.

Have a look at my template:
https://forums.unrealengine.com/unreal-engine/marketplace/1580543--endless-runner-shooter-template