I want to first off apologize if this thread is in the wrong spot. I really wasn’t sure where to put this, because I’m not sure if this is something you do with code, or within the unity editor itself. I have my camera attached to my character so that when I move it follows me around, but I want the camera to stop moving once it reaches the boundaries, this is a 2D game with a top down view, I have 4 walls, one to the left, right, top, and bottom, the character cannot pass through these walls as inside of them is the game area. However when the character is near a wall it exposes the outside of the map, I want the camera to stop moving when it touches one of the walls so that it does not expose anything beyond the walls. I wouldn’t even know where to begin with this so I do not have any code currently written.
It’s likely a script, but a small one.
If the camera is “attached” as a child of the player, then detaching the camera will stop the current “move together” behavior.
I would set up a zone like a cube collider as a trigger, and put a script on that zone. If the player enters the trigger zone, set the parent of the camera to null.
If you want the camera to remain “attached” but also avoid certain areas due to it showing things it shouldn’t, that gets more complicated.
After thinking about it for a bit I figured I might be able to use Mathf.clamp to restrict the cameras boundaries on how far it could move, and it works however theres a lot of jittering going on, is there any way I could fix the jittering? If not I’ll look into a different solution, such as the one you provided.
if (camera.pos > max) camera.pos = max;
if (camera.pos < min) camera.pos = min;
I did something very very similar to this. I added invisible barriers in the game that the player can pass through but the camera cannot by creating a script for that camera that sets the position of the camera to the position of the barrier if the camera tries to set it. Thank you!