UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionNewbie needs help with some code :O EmptyNewbie needs help with some code :O

more_horiz
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

descriptionNewbie needs help with some code :O EmptyRe: Newbie needs help with some code :O

more_horiz
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.
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply