Player in center of screen and camera restriction

k so 2 details, game is 2D bird’s eye view shooter, and the code below is unfinished.

Basically wat i want to do is have my player always in the center of the screen unless i go near the edge of the screen(similar to as in Mario when u try to go to the left side of the screen the camera doesnt follow u anymore unitl u get back to the center of the screen)

for that, all i do is remove the camera object from the parent object into its own space showing in the code below. this code work but only when i got to the right side of the screen for some reason.(i know i havent coded for the y axes but if i do then it doesnt work at all)

any help?

also as u can see im using fixed number to say where the restrictions starts which wont work if u change resolution of the game so i need a fix for that too, i tried using screen.width and height but also didnt work.

function LateUpdate () {

	transform.position = new Vector3(Mathf.Clamp(transform.position.x, -27, 27), Mathf.Clamp(transform.position.y, -36, 36), transform.position.z);
	
	if(player.transform.position.x <= Mathf.Clamp(transform.position.x, -27, 27))
	{
		transform.parent = player.transform;
	}
	else
	{
		transform.parent = null;
    }
}

What I would do as dannyskim said is move the transform position of the camera to the character. So On a script that is attached to the player add this:

void Start () {
	mainCamera = (GameObject) GameObject.FindWithTag ("MainCamera");
	
	}
void Update () {
    mainCamera.transform.position = new Vector3(transform.position.x,10,transform.position.z);
    }

And then on a script attached to the main camera add:

transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftLimit, rightLimit), transform.position.y, transform.position.z);

In the Update somewhere, where left limit and right limit are public floats you have defined at the top of the script.

Note that this is done in C# but is very easily converted to Unity Script, if you look at the documentation.

Peace,