- Home /
Creating endless room for endless falling game
I'm using this tutorial to practice creating an endless runner game : http://www.raywenderlich.com/69544/make-game-like-jetpack-joyride-unity-2d-part-2
But now I want to make a vertical endless which has the character falling down.
I have this code but the rooms weren't spawning correctly.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class GenerateLevel : MonoBehaviour { Prefabs public GameObject[] availableRooms;
public List<GameObject> currentRooms; private float screenWidthInPoints; void Start () { float height = 2f * Camera.main.orthographicSize;
screenWidthInPoints = height * Camera.main.aspect; } void AddRoom(float farhtestRoomEndY) {
int randomRoomIndex = Random.Range(0, availableRooms.Length);
GameObject room = (GameObject)Instantiate(availableRooms[randomRoomIndex]);
float roomHeight = room.transform.FindChild("floor").localScale.y;
float roomCenter = farhtestRoomEndY + roomHeight * 0.5f;
room.transform.position = new Vector3(0, roomCenter, 0);
currentRooms.Add(room); } void GenerateRoomIfRequired() { List<GameObject> roomsToRemove = new List<GameObject>();
bool addRooms = true;
float playerY = transform.position.y;
float removeRoomY = playerY - screenWidthInPoints;
float addRoomY = playerY + screenWidthInPoints;
float farthestRoomEndY = 0;
foreach(var room in currentRooms) { float roomHeight = room.transform.FindChild("floor").localScale.y; float roomStartY = room.transform.position.y - (roomHeight * 0.5f); float roomEndY = roomStartY - roomHeight;
if (roomStartY > addRoomY)
addRooms = false;
if (roomEndY < removeRoomY)
roomsToRemove.Add(room);
farthestRoomEndY = Mathf.Min(farthestRoomEndY, roomEndY); }
foreach(var room in roomsToRemove) { currentRooms.Remove(room); Destroy(room); }
if (addRooms) AddRoom(farthestRoomEndY); } void Update () {
} void FixedUpdate () { GenerateRoomIfRequired(); } }
What's wrong with the code?
Any help with this? Or can anyone gives an example on how to make an endless vertical room? thanks
Well, one thing that's wrong with your code is that it's completely unreadable. Try adding some returns (and comments) in there so we can actually read your code.
Your answer
Follow this Question
Related Questions
getting udp package info inside unity (GlovePIE) 0 Answers
Will Facebook Gameroom SDK work with mobile as well? 0 Answers
change gravity after time 1 Answer
In Game Play, FPC automatically falls through terrain! 1 Answer
Spawn Troubles... 1 Answer