Stop camera when wall hit 2D game

Hello guys. I am trying to make the camera stop following the player, when it collides with a wall.
Here’s the script and an explanation after:

using UnityEngine;
using System.Collections;

public class CameraControl : MonoBehaviour {
	
	public Player player;

	public bool isFollowing;
	
	public float xOffset;
	public float yOffset;
	
	
	void Start () {
		player = FindObjectOfType<Player> ();
		
		isFollowing = true;
		
		
	}
	
	
	void Update () {
		
		if (isFollowing)
			transform.position = new Vector3 (player.transform.position.x + xOffset, player.transform.position.y + yOffset, transform.position.z);
		
	}

	void OnTriggerEnter2D(Collider2D other)
	{
	

			isFollowing = false;
	}

	void OnTriggerExit2D(Collider2D other){

		isFollowing = true;
	}
}
  • Attach Box Collider2D and a Rigidbody2d to the camera.
  • Made 2 box colliders on the wall - 1 trigger, 1 not.

the screen starts shaking on collision. I know it is because I should use velocity or addforce in order to move the camera, since the transform.translate teleports the object. However I am really comfortable with this script. So I want to ask you, awesome guys, if there is a way, to stop the camera when it collides (hits) the wall in a room (for example), so that the player moves freely to the edge of the screen, but the camera stays in one place, until he returns to the middle of the screen, then the camera follows again.

Any help is greatly appriciated!

i think its somethign like this you need

	// player
	public Transform playerToFollow;
	
	// walls
	public Transform Firstwall;
	public Transform Lastwall;

	// camera rules
	public float Height;
	public float Zoom;
	public float max;
	public float min;
	
	void Start () {
		min = Firstwall.position + 2f;
		max = Lastwall.position - 2f;
	}
	
	void Update () {

		// if players position > min and player position < max
		if (playerToFollow.position.x > min && playerToFollow.position.x < max)
		{
			// change camera's position = players position.x height.y zoom.z
			transform.position = new Vector3 (playerToFollow.position.x, Height, Zoom);				
		}
	}

in this script i simple Check in my Update if the Players Position is bigger than min and smaller than max if both conditions are met the camera will follow the player if the conditions are not met the camera will stop following the player.

Thanks lloladin for the answer. Simple and easy, and smart!
I’m suprised, currently, there isn’t anything on the web about this simple issue. A lot of games have this feature, and yet, not in the forums or here, there was a simple answer to that particular question.

Here is my finished script, there were some simple mistakes in the upper one:

using UnityEngine;
using System.Collections;

public class CameraControl : MonoBehaviour {
	
	public Player player;

	public float xOffset;
	public float yOffset;

	public Transform firstWall;
	public Transform lastWall;

	public float Height;
	public float Zoom;
	public float max;
	public float min;
	
	void Start () {
		player = FindObjectOfType<Player> ();

		min = firstWall.transform.position.x + 2f;
		max = lastWall.transform.position.x - 2f;
	}
	
	
	void Update () {

		if (player.transform.position.x > min && player.transform.position.x < max)
		{
			// change camera's position = players position.x height.y zoom.z
			transform.position = new Vector3 (player.transform.position.x + xOffset, player.transform.position.y + yOffset, Zoom); 
		
		}

	}

}