This may be a lil stupid, but I’m making an endless runnner and I can’t make my obstacles spawn properly…
To elaborate, I have 3 different obstacles, and I can’t get them to be picked at random, as the next to appear – if that makes sense!
I also can’t get the distance between each obstacle to be random, either.
For you to get an idea of what kind of game I’m making, I’m trying to make it somewhat of the same layout as Chome Dino (https://chromedino.com)
Note: I would provide some code, but I have nothing for this issue – I just kinda need some source code that will work ;p I’ve been spending several days trying to find some code that works, but nothing so far.
Oh, I do have 2 classes that are my Obstacle generator and the jumping function.
If code is needed, I’m happy to provide it!
Everything I’ve researched usually were for Unity, which I’ve had some insane frustrations with, hence why I’m not using it.
I tried making an array that would hold my 3 obstacles, with the idea of picking an index at random, but that threw the error CS0021 – "Cannot apply indexing with [] to an expression of type ‘ObstacleGenerator’.
I also tried having:
int obstacles = random.Next(0, 3);
Then saying:
if (obstacles == 0)
{
obstacle1.Update(gameTime);
}
else if (obstacles == 1)
{
obstacle2.Update(gameTime);
}
else if (obstacles == 2)
{
obstacle3.Update(gameTime);
}
In theory, that should pick a number at random (between 0 and 3), and whichever is picked, that’s the obstacle that should start moving. The issue is that the random.Next() doesn’t just pick one random number – it’s constantly picking, so the obstacles move, but jerkily. Is that what’s supposed to happen with random.Next ???
If so, is there a better way to get the enemies to get picked at random?
I’m using C#, Visual Studio with a MonoGame template. Any help is appreciated! 🙂
2
Answers
I have some attached code here, hopefully, this can be of help:
Code for obstacle generation starting on Line 148:
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/Ground.cs
I have falling grounds in this endless runner. This code moves my obstacles based on my ground height (y-axis):
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/GroundFall.cs
This code tracks my obstacles position and destroys it when it goes off-screen:
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/Obstacle.cs
You can use the random.Range() function to generate numbers.
Just a suggestion, why not use a Queue structure for your obstacles. Enqueue an obstacle when it spawns on screen and then in your main Update loop, iterate through the Queue with a foreach loop to update each obstacle. When an obstacle leaves the screen Dequeue it.
A Queue seems perfect because it obeys FIFO rules (first in first out) which corresponds to your obstacles entering and leaving the playing field.
As for when to spawn them you could keep it simple for now, have a decrementing counter in your main Update loop, when it reaches zero, spawn another random obstacle and add it to the Queue; at the same time reset the counter to a random number between say 200 and 400 so you get another obstacle at a random time in the future.