How to stop the camera when the player has reached the edge of the level?

I have viewed some tutorials and they do work but it’s a bit finicky and the camera does not re-center on the player when respawned.

I’m making a 2D platformer game where the camera is centered on the player and follows the player around. However, I don’t want to make the camera see past the edges of the level but I still want the player to be able to move around and touch the edge. Also, the camera should follow the player around again once the camera is centered on the player again. Can someone please explain how I can go about this? Thanks.

Im not sure, but try to use a simple operation with Mathf.Clamp combined with the Update().

I would try to fix such kind of issue by limiting the camera move using a Rect, or rather better, a Collider2D set as “isTrigger” that covers the whole level and defines their bounds (or better said, bounds that cam shouldn’t cross).

So, with the “trigger collider” defined, you can attach this to the cam’s script to control events when the cam goes out of the “trigger collider” (ALERT: The snippet is in Javascript. You might need to convert it’s syntax to C# if you are using C#):

function OnTriggerExit2D(other : Collider2D) {
    // I assume your cam has the tag "MainCamera" attached
    if (other.tag == "MainCamera") {
        /*  Whatever logic you might need to implement in order to limit the camera's moves.
         *  You might need to do coord checks to see if cam went out of bounds by right,
         *  left, up and down, and limit it in a different way depending on this
         */
    }
}

I strongly reccommend to read carefully my snippet rather than copy-pasting blindly. Is much more like a template rather than a complete snippet of code. I would complete it, but due to I diidn’t see your code, I can only guess how you made it.

It might also be helpful for you to see the features of OnTriggerExit2D method, as well as OnTriggerEnter2D and OnTriggerStay2D (all them methods of the MonoBehaviour class), as well as the features of Collider2D class.

And, in general, you can find complete documentation about any class or method used by unity in docs.unity3d.com