Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Newbie needs help with some code :O

Newbie needs help with some code :O

Go down  Message [Page 1 of 1]

1 Newbie needs help with some code :O on Wed May 20, 2015 7:43 am


Member [Lv. 0]
Member [Lv. 0]
Greets Guy,
im currently building a flappy bird clone , i had it working but wanted to improve the room generation.

void Start () {

float height = 2.0f * Camera.main.orthographicSize;
screenWidthInPoints = height * Camera.main.aspect;

void AddRoom(float farthestRoomEndX)
int randomRoomIndex = Random.Range (0, availableRooms.Length);
GameObject room = (GameObject)Instantiate (availableRooms [randomRoomIndex]);
float roomWidth = room.transform.FindChild ("floor").localScale.x;
float roomCenter = farthestRoomEndX + roomWidth * 0.5f;
room.transform.position = new Vector3 (roomCenter, 0, 0);

void GenerateRoomIfRequired()
List<GameObject> roomsToRemove = new List<GameObject>();

bool addRooms = true;

float playerX = transform.position.x;

float removeRoomX = playerX - screenWidthInPoints;

float addRoomX = playerX + screenWidthInPoints;

float farthestRoomEndX = 0;

foreach(var room in currentRooms)
float roomWidth = room.transform.FindChild("floor").localScale.x;
float roomStartX = room.transform.position.x - (roomWidth * 0.5f);
float roomEndX = roomStartX + roomWidth;

if (roomStartX > addRoomX)
addRooms = false;

if (roomEndX < removeRoomX)
farthestRoomEndX = Mathf.Max(farthestRoomEndX, roomEndX);


foreach(var room in roomsToRemove)

if (addRooms)
// Update is called once per frame
void FixedUpdate () {
GenerateRoomIfRequired ();


Whats currently happening is that Unity creates a thousand rooms and just keeps creating and i rly dont know why, yet?
if someone could help that would be great Smile thanks

View user profile

2 Re: Newbie needs help with some code :O on Fri Sep 11, 2015 7:32 pm


Member [Lv. 3]
Member [Lv. 3]
you are running void

FixedUpdate () {
GenerateRoomIfRequired ();

this will call GenerateRoomIfRequired every Fixed Update.

need to stop it from generating new rooms every frame thats all.

View user profile http://jakeaquilina.weebly.com/

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum