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!


No replies. Dang it
– ToxicTurtle420I'm having the exact same problem, 2 years on from this post. The 0 replies is daunting. Did you ever manage to resolve this?
– BeGunityAre you getting an error in the case where the difference is negative? Because -1 < 0.5 is true but may not be the logic you're looking for.
– PharanI've had Debug.Log format numbers for me (don't recall under what circumstances), but is Difference just displaying as 0.5f in the log, but it's really 0.49999f or something? I'd go with setting a break point on that line and do a watch on the value once you get there and see what the actual value is. Or try using [string.Format][1] when logging the value to make sure you're getting all of the decimal places. e.g.
– Dave-CarlileDebug.Log(string.Format("{0:0.00000000}", Difference))or something like that. [1]: https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx