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
Share
Go down
Member [Lv. 0]
Member [Lv. 0]
Posts : 1
Points : 1161
Rep : 0
Join date : 2015-05-20
View user profile

Newbie needs help with some code :O

on Wed May 20, 2015 7:43 am
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);
currentRooms.Add(room);
}

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)
roomsToRemove.Add(room);
farthestRoomEndX = Mathf.Max(farthestRoomEndX, roomEndX);

}

foreach(var room in roomsToRemove)
{
currentRooms.Remove(room);
Destroy(room);
}

if (addRooms)
AddRoom(farthestRoomEndX);
}
// 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
avatar
Member [Lv. 3]
Member [Lv. 3]
Posts : 45
Points : 1398
Rep : 3
Join date : 2015-08-30
Age : 23
Location : Melbourne
View user profilehttp://jakeaquilina.weebly.com/

Re: Newbie needs help with some code :O

on Fri Sep 11, 2015 7:32 pm
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.
Back to top
Permissions in this forum:
You cannot reply to topics in this forum