- Home /
Trying to create an endless vertical level
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.
Answer by GarretLawrence · Jan 23, 2017 at 08:24 AM
I have made a simple demo for you. Try it
Use Left/Right Arrow key to move Left/Right.
Thanks a lot, but could you briefly explain how to set this demo up? Thanks in advance.
I'm so sorry and really don't want to annoy you, but when I try to attach the script, the editor tells me there's an error with the following line of code:
lastSpawnedBG = Instantiate(bgList[Random.Range(0, bgList.Count)], spawnPos,Quaternion.identity);
It tells me: "Assets/EndlessRun/EndlessRun.cs(36,13): error CS0266: Cannot implicitly convert type UnityEngine.Object' to
UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)".
Do you have a solution for that? Really sorry for annoying you like that for such a simple question. Thanks in advance. (Hopefully the last time)
This works perfectly, when set up in a 3D Project with normal BoxCollider and RigidBodys (thanks a lot), but when I try to set it up in a 2D Project with 2D BoxCollider and RigidBody2D it somehow doesn't work. Do you have an idea about how to solve this? Thanks in advance.
Answer by srikaran_p · Jan 23, 2017 at 12:24 PM
Just do whatever you have been doing on the x - axis to y-axis.
Like make the player move from down to up.
Secondly, if you move the level then you would get a pattern of obstacles which would make the game boring. So, creating and destroying obstacles randomly would be great.
Finally, instantiate your rooms (or obstacles) on the y-axis.
Personally, I don't like to modify codes because that is a long process. Why don't you try to make your own code so that you would know what each statement does. All the Best.