Hi guys,
I got to code an app for my school project. I’m trying to learn how to code in C# through following this tutorial: https://www.raywenderlich.com/69544/make-game-like-jetpack-joyride-unity-2d-part-2 . In the tutorial, although a endless level is created, it’s horizontal, opposed to what I’m looking for. So I took the code and modified it, that as far as I know, it should work, but it does not. Might be important to know, that the player moves, not the background. Here is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GeneratorScript : MonoBehaviour {
public GameObject[] availableRooms;
public List<GameObject> currentRooms;
private float screenHeightInPoints;
// Use this for initialization
void Start () {
screenHeightInPoints = 2.0f * Camera.main.orthographicSize;
}
void GenerateRoomIfRequired()
{
List<GameObject> roomsToRemove = new List<GameObject>();
bool addRooms = true; // will be set false most of the time in the for part
float playerY = transform.position.y; // getting the players position
float removeRoomY = playerY - screenHeightInPoints; // the point a room should be removed (isn't seen by the player);
float addRoomY = playerY + screenHeightInPoints; // the point a room should be added
float farthestRoomEndY = 0;
foreach(var room in currentRooms)
{
float roomHeight = room.transform.FindChild("bg").localScale.y + room.transform.FindChild("bf_window").localScale.y; // setting the room height by adding the heigth of both parts of the room
float roomStartY = room.transform.position.y - (roomHeight * 0.5f); //setting the start of a room
float roomEndY = roomStartY + roomHeight; //setting the end of a room
if (roomStartY > addRoomY)
addRooms = false; // no room should be added
if (roomEndY < removeRoomY)
roomsToRemove.Add(room); // room should be deleted
farthestRoomEndY = Mathf.Max(farthestRoomEndY, roomEndY); // setting the end of the room
}
foreach(var room in roomsToRemove)
{
currentRooms.Remove(room); //destroying the rooms
Destroy(room);
}
if (addRooms)
AddRoom(farthestRoomEndY); //adding the room
}
void AddRoom(float farhtestRoomEndY)
{
int randomRoomIndex = Random.Range(0, availableRooms.Length); //choosing a random prefab ot of the availableRooms array
GameObject room = (GameObject)Instantiate(availableRooms[randomRoomIndex]); //creating that prefab
float roomHeight = room.transform.FindChild("bg").localScale.y + room.transform.FindChild("bg_window").localScale.y; //getting the height of both textures to set the room height
float roomCenter = farhtestRoomEndY + roomHeight * 0.5f; // setting the center height of the room one wants to add, adding half the room height to the end of the room
room.transform.position = new Vector3( 0, roomCenter, 0); // moving the new room on top of the old room
currentRooms.Add(room); // storing the newly created room in the currentRooms array
}
void FixedUpdate ()
{
GenerateRoomIfRequired();
}
}
On top, I read that it’s better to simply move the rooms instead of destroying and creating them, does someone have an idea or even solution on that? Excuse my English, I’m just a student from Germany.
Thanks in advance.