unity3d - Random object in Unity -
i want make endless runner game. have 2 objects, 1 on top , 1 on bottom. players have jump between or squat on objects. made script creates objects, 2 object created on same position, players can't anything. how solve that? can check other objects on axis x, not collider?
basically asking give our level generator , game controller source code! have write 1 game. not because don't want give code because each game must have own.
for starters can:
split game area matrix have array of sort, each cell can have game object or empty. game objects can have own local position within cell. 1 cell can't contain 2 game objects.
have level generator tells game controller should spawn new objects. should implement in level generator prevent overlaps.
look @ psudo code:
void fixedupdate() { if (player.transform.position.x + half_of_screen_width_plus_margin > nextx) { spawn(tmp[i].prefab, nextx); nextx += tmp[i].distancetonext; i++; } }
half_of_screen_width_plus_margin
game foresee what's coming
tmp[]
collection of (not-instantiated) objects instantiated. each object defined arbitrarily.
as see script checks next position every fixeddeltatime seconds, , compares position x of end of screen next position x. if passed creates next object , change next position x further location.
if want use random generation tmp[] should change tmp. each instantiation next generated:
void fixedupdate() { if (player.transform.position.x + half_of_screen_width_plus_margin > nextx) { spawn(tmp.prefab, nextx); nextx += tmp.distancetonext; tmp = generate_new_random_object(); } }
Comments
Post a Comment