Restrain Scrolling camera from leaving background

I am making a side scrolling game where the player is able to move left and right. The background scrolls with the player using this script

Scrolling background Script

    void FixedUpdate ()
    {       
        Vector3 point = Camera.main.WorldToViewportPoint(target.position);
        Vector3 delta = target.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f,1));
        Vector3 destination = transform.position + delta;

        destination.y = 0;
        Vector3 velocity = Vector3.zero;

        transform.position = destination;
        //Debug.Log(destination);
    }

I am having two problems with this however as I have more then 1 background sprite as the character progresses through the level (tiled if you will). I have them name for example “bg1”,“bg2”,“bg3” what I need to accomplish is only allowing the camera to scroll in a positive direction while the player can still be at the edge of the Viewport. As you will see in the next script for the first background tile I can simply set the cam’s X position to 0 and that works fine. However, when the camera stops from the conditional statement it snaps into place and when it passes the conditional statement it snaps back giving the game a very jagged appearance.
Here is the script below.

check bounds script

public class scriptCheckBounds : MonoBehaviour {   

    private float _xMin;
    private float _xMax;

    void Update()
    {       
        float _xMin = GameObject.Find("bg1").GetComponent<SpriteRenderer>().bounds.min.x;
        float _camMinX = Camera.main.ViewportToWorldPoint(new Vector3(0, 0)).x;

        Debug.Log(_xMin);

        if (_camMinX <= _xMin)
        {
            float _vel = 0.0f;
            Debug.Log("bad cam");
            var _camPos = Camera.main.transform.position;
            _camPos.x = 0;           
            Camera.main.transform.position = _camPos;
        }
        else
        {
            Debug.Log("good cam");
        }
    }
}

So to sum it up my questions are as follows:

  • How will I set the camera to stay in the center of each up coming tile as the player passes into it.
  • How do I avoid having the camera snap into place when the conditional statement is fired.

I appreciate the help and time from the community thank you very much!

If I understand your question, and I’m not 100% sure I do, you want the camera not to snap to set points, but to sort of slide to the correct position and then stay there? And once the character crosses the viewport boundary, you want to snap to the next area, is that correct? So we’re talking Legend of Zelda/Megaman?

Have you looked into Linear Interpolation (lerp)? Unity has a really nice set of functions for dealing with moving things from point A to point B smoothly.

in a sense that is what I am talking about but I’m not really wanting the camera to snap to certain points. Let me see if I can give you an example.

player is in the first background tile and starts to move forward and then decides to start moving backward. I only want the camera to go back far enough to show the left boundary of the first background tile.

Now lets say the player then starts to move forward again and the camera’s view port is touching both bg1 and bg2 and the player again decides to move backward. I then would want the camera to only go back far enough so that essentially the character can’t go back to the beginning of the level.

** my thought process on this was using a foreach loop to iterate through the bg tiles and see what ones are visible but mathematically I have no clue on how to calculate where the camera should position itself.

Also with what you said about snapping, I do not want it to just violently snap into place I want it to in a sense slide into place to create a smooth transition between the camera scrolling and stopping.

You just need to keep track of the maximum left position the camera is allowed to go to, aka. the minimum x value it’s allowed to have. Call it CameraXMin or something and increase it’s value based on certain criteria. To get the camera smoothly following, you just have to give it acceleration and deceleration. It’s not really complicated, the harder part is understanding exactly what you want it to do.

So what you are saying is as the background tiles become visible for example I would increase the CameraXmin’s value?

as far as adding velocity to the camera could you possibly give me a small example of how you would go about that or a reference to something that I could try and understand?