I am trying to create an endless runner. I have already created a system to generate infinite backgrounds from an array of some. However, on running the game, I get huge black bar on the right side of the screen, both on PC and on Android phone.
Here are the screenshots :
(On PC - In Unity Game Preview)
(On Android device)
One more thing, on Android, it doesn’t even display the left side of the game, i.e, where the character is. It looks like the things are displaced towards left.
Also, the game is a scrolling on and everything is scrolling but behind that bar. It just doesn’t go.
I hope I can get some help. Ask me if you need any piece of code.
Here are two scripts which I think might have the bug.
CameraFollow.cs
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public GameObject targetObject;
private float distanceToTarget;
void Start () {
distanceToTarget = transform.position.x - targetObject.transform.position.x;
}
void Update () {
float targetObjectX = targetObject.transform.position.x;
Vector3 newCameraPosition = transform.position;
newCameraPosition.x = targetObjectX + distanceToTarget;
transform.position = newCameraPosition;
}
}
GeneratorScript.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GeneratorScript : MonoBehaviour
{
public GameObject[] availableRooms;
public List<GameObject> currentRooms;
private float screenWidthInPoints;
void Start () {
float height = 0.0f * Camera.main.orthographicSize;
screenWidthInPoints = height * (Camera.main.targetDisplay);
}
void FixedUpdate()
{
GenerateRoomIfRequred ();
}
void AddRoom(float farhtestRoomEndX)
{
int randomRoomIndex = Random.Range (0, availableRooms.Length);
GameObject room = (GameObject)Instantiate (availableRooms [randomRoomIndex]);
float roomWidth = room.transform.FindChild ("floor").localScale.x;
float roomCenter = farhtestRoomEndX + roomWidth * 3.0f;
room.transform.position = new Vector3 (roomCenter, 0, 0);
currentRooms.Add (room);
}
void GenerateRoomIfRequred()
{
//1
List<GameObject> roomsToRemove = new List<GameObject>();
//2
bool addRooms = true;
//3
float playerX = transform.position.x;
//4
float removeRoomX = playerX - screenWidthInPoints;
//5
float addRoomX = playerX + screenWidthInPoints;
//6
float farhtestRoomEndX = 0;
foreach(var room in currentRooms)
{
//7
float roomWidth = room.transform.FindChild("floor").localScale.x;
float roomStartX = room.transform.position.x - roomWidth * 6.0f;
float roomEndX = roomStartX + (roomWidth * 13.0f);
//8
if (roomStartX > addRoomX)
addRooms = false;
//9
if (roomEndX < removeRoomX)
roomsToRemove.Add(room);
//10
farhtestRoomEndX = Mathf.Max(farhtestRoomEndX, roomEndX);
}
//11
foreach(var room in roomsToRemove)
{
currentRooms.Remove(room);
Destroy(room);
}
//12
if (addRooms)
AddRoom(farhtestRoomEndX);
}
}
I tried playing with the numbers on GeneratorScript, but nothing seemed to help.
Also, I also tried changing aspect ratio and all that stuff, but the black bar seems to stay in place and everything else just changes.
I don’t know, I don’t want to create a new Project and import everything there.
Can someone please give me a solution to this problem as soon as possible?
Also, I am a beginner at game development and a designer by profession.
Thank you,
Have a nice day!