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;
    }
}

I would suggest not parenting the camera at all, just set the transform.position of the camera to follow the position of the character. Doing this has much more predictable results and isn't necessarily reasonably more expensive to do. Also, take a look at the free Unity Lerpz platformer example, it has a pretty standard smooth follow camera script that you can create your basis off of. http://unity3d.com/support/resources/tutorials/3d-platform-game.html

ok late reply but i checked the Lerpz game and the code seems rather complicated + since it deals with changing camera restrictions and changing players it makes it even harder to understand. i tired fixing some things but im very much stuck right now cause there's more problems with the way im doing things as well. i dont usually ask for that much but i dont rly know where i should start, will try look more into that other game but dunno if im gonna gonna make sense of it much; but essentially the behavior of my camera is basically the same as the #D platform tutorial game

1 Answer

1

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,

No problem. I suggest taking the answer as the correct one to declutter the unanswered and if you have any other problems regarding a different part of coding to make a new question so that the rest of the UA community can help answer it for you.