Camera Boundry

So I’ve been trying to set up a platformer game, and I’ve been trying to setup a camera similar to games like Mario or Megaman, where it won’t scroll outside the current level. Basically, the idea is that I first cast a ray from the corner of the screen. If it doesn’t hit the background, another ray is cast towards the background. It uses the distance to the background to calculate how far to move back. Unfortunately, this doesn’t work to well, as the camera instead bounces back and forth. Here’s the script:

public GameObject Player;
public GameObject Background;
public LayerMask mask;
private float pointDistanceX;

void FixedUpdate ()
{
    Ray topRight = Camera.main.ScreenPointToRay(new Vector3(Screen.width, Screen.height, 10));
    Ray topLeft = Camera.main.ScreenPointToRay(new Vector3(0, Screen.height, 10));
    Ray botLeft = Camera.main.ScreenPointToRay(new Vector3(0, 0, 0));
    Ray botRight = Camera.main.ScreenPointToRay(new Vector3(Screen.width, 0, 10));
    RaycastHit hit;
    Mesh mesh = Background.GetComponent<MeshFilter>().mesh;
    transform.position = Player.transform.position + new Vector3(pointDistanceX, 0, -10);
    if (!Physics.Raycast(topRight, mask))
    {
        if (Physics.Raycast(new Vector3(topRight.origin.x, topRight.origin.y, 0), Vector3.left, out hit, mask))
        {
            pointDistanceX = -Vector3.Distance(hit.point, new Vector3(topRight.origin.x, topRight.origin.y, 0));
            Debug.DrawRay(new Vector3(topRight.origin.x, topRight.origin.y, 0), Vector3.right * pointDistanceX, Color.yellow);
        }                
    }
    while (transform.position.x < -0.1f)
    {
        transform.Translate(new Vector3(0.1f, 0, 0) * Time.deltaTime);
    }
    while (transform.position.y > 2)
    {
        transform.Translate(new Vector3(0, -0.1f, 0) * Time.deltaTime);
    }
    while (transform.position.y < -13.5)
    {
        transform.Translate(new Vector3(0, 0.1f, 0) * Time.deltaTime);
    }
}

Well I finally figured it out myself! For one thing, the script now starts by going to player position. Then it casts the rays, and AFTER that adjusts position based on the raycasts. That way the rays always start at the same place, and it never is shown off the edge of the screen. I’m not the best at explaining code XD, so just look at the finished code if you don’t understand.

public GameObject Player;
public GameObject Background;
public LayerMask mask;
private float pointDistanceX;
private float pointDistanceY;

void FixedUpdate()
{
    transform.position = Player.transform.position;
    Ray topRight = Camera.main.ScreenPointToRay(new Vector3(Screen.width, Screen.height, 10));
    Ray topLeft = Camera.main.ScreenPointToRay(new Vector3(0, Screen.height, 10));
    Ray botLeft = Camera.main.ScreenPointToRay(new Vector3(0, 0, 0));
    Ray botRight = Camera.main.ScreenPointToRay(new Vector3(Screen.width, 0, 10));
    RaycastHit hit;
    Mesh mesh = Background.GetComponent<MeshFilter>().mesh;
    if (!Physics.Raycast(topRight, mask))
    {
        if (Physics.Raycast(new Vector3(topRight.origin.x, topRight.origin.y, 0), Vector3.left, out hit, mask))
        {
            pointDistanceX = -Vector3.Distance(hit.point, new Vector3(topRight.origin.x, topRight.origin.y, 0));
        }
    }
    if (!Physics.Raycast(botRight, mask))
    {
        if (Physics.Raycast(new Vector3(botRight.origin.x, botRight.origin.y, 0), Vector3.left, out hit, mask))
        {
            pointDistanceX = -Vector3.Distance(hit.point, new Vector3(botRight.origin.x, botRight.origin.y, 0));
        }
    }
    if (!Physics.Raycast(topLeft, mask))
    {
        if (Physics.Raycast(new Vector3(topLeft.origin.x, topLeft.origin.y, 0), Vector3.right, out hit, mask))
        {
            pointDistanceX = Vector3.Distance(hit.point, new Vector3(topLeft.origin.x, topLeft.origin.y, 0));
        }
    }
    if (!Physics.Raycast(botLeft, mask))
    {
        if (Physics.Raycast(new Vector3(botLeft.origin.x, botLeft.origin.y, 0), Vector3.right, out hit, mask))
        {
            pointDistanceX = Vector3.Distance(hit.point, new Vector3(botLeft.origin.x, botLeft.origin.y, 0));
        }
    }
    if (!Physics.Raycast(topLeft, mask))
    {
        if (Physics.Raycast(new Vector3(topLeft.origin.x, topLeft.origin.y, 0), Vector3.down, out hit, mask))
        {
            pointDistanceY = -Vector3.Distance(hit.point, new Vector3(topLeft.origin.x, topLeft.origin.y, 0));
        }
    }
    if (!Physics.Raycast(topRight, mask))
    {
        if (Physics.Raycast(new Vector3(topRight.origin.x, topRight.origin.y, 0), Vector3.down, out hit, mask))
        {
            pointDistanceY = -Vector3.Distance(hit.point, new Vector3(topRight.origin.x, topRight.origin.y, 0));
        }
    }
    if (!Physics.Raycast(botLeft, mask))
    {
        if (Physics.Raycast(new Vector3(botLeft.origin.x, botLeft.origin.y, 0), Vector3.up, out hit, mask))
        {
            pointDistanceY = Vector3.Distance(hit.point, new Vector3(botLeft.origin.x, botLeft.origin.y, 0));
        }
    }
    if (!Physics.Raycast(botRight, mask))
    {
        if (Physics.Raycast(new Vector3(botRight.origin.x, botRight.origin.y, 0), Vector3.up, out hit, mask))
        {
            pointDistanceY = Vector3.Distance(hit.point, new Vector3(botRight.origin.x, botRight.origin.y, 0));
        }
    }
    transform.position = transform.position + new Vector3(pointDistanceX, pointDistanceY, -10);        
}