1
\$\begingroup\$

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

on Android

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.

EDIT :

"CameraFollow" script

 using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {

    public GameObject targetObject;
    private float distanceToTarget;

    // Use this for initialization
    void Start () {
        distanceToTarget = transform.position.x - targetObject.transform.position.x;

    }

    // Update is called once per frame
    void Update () {


        float targetObjectX = targetObject.transform.position.x;

        Vector3 newCameraPosition = transform.position;
        newCameraPosition.x = targetObjectX + distanceToTarget;
        transform.position = newCameraPosition;
    }
}

My "GeneratorScript"

 using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GeneratorScript : MonoBehaviour {

    public GameObject[] availableRooms;

    public List<GameObject> currentRooms;

    private float screenWidthInPoints;





    // Use this for initialization
    void Start () {
        float height = 0.0f * Camera.main.orthographicSize;
        screenWidthInPoints = height * (Camera.main.targetDisplay);
    }

    // Update is called once per frame
    void Update () {

    }

    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.

\$\endgroup\$
6
  • \$\begingroup\$ Looks cool. By the way, it's too difficult to say where is a problem, because it can be anywhere - in code, in scene, in camera setting, in UI. To get proper answer you need to provide more information about your project. \$\endgroup\$
    – Exerion
    Sep 18, 2015 at 10:59
  • \$\begingroup\$ Provide the code used to set your viewport and scrolling. This would help in determining the cause of the problem. \$\endgroup\$
    – jgallant
    Sep 18, 2015 at 12:30
  • \$\begingroup\$ I will when I get back home. \$\endgroup\$
    – Hardik
    Sep 18, 2015 at 14:05
  • \$\begingroup\$ @Jon Code added. \$\endgroup\$
    – Hardik
    Sep 18, 2015 at 16:06
  • \$\begingroup\$ @Exerion Hello, please help. I have added the codes in the main post. \$\endgroup\$
    – Hardik
    Sep 19, 2015 at 4:11

1 Answer 1

1
\$\begingroup\$

I know this is 8 years later, but I found that for me this black bar was from adjusting the viewport rect settings to make the camera smaller. I reversed it by resetting viewport rect back to X-0 Y-0 W-1 H-1

The main reason I am reporting it here is because I could not find anything else online and could have benefited from this earlier.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .