- Home /
Camera to centre on player, but never exceed "map" bounds
I am creating a game which has up to 4 orthographic cameras (for up to 4 players), which take up various amounts of the screen, depending on the amount of players.
I have a script which controls all the cameras, setting their position relative to the players they are watching.
What I want to achieve is a simple overhead-runner style of movement, whereby the camera will follow the player, but not go outside the bounds of the map.
I have the code to move the cameras, within a specific bounds, already in place. The question is, how do I FIND the minimum and maximum values? I have a Map object, placed at 0,0 - so I guess I need need to somehow find the top and bottom corners of that object, in relation to the cameras. How can I do this?
My current code is below:
if((trackPlayer1 == null))
{
camPlayer1.transform.position = this.transform.position;
}
else
{
camPlayer1.transform.position = new Vector3(Mathf.Clamp(trackPlayer1.transform.position.x, minX, maxX), Mathf.Clamp(trackPlayer1.transform.position.y, minY, maxY), camPlayer1.transform.position.z);
}
if((trackPlayer2 == null))
{
camPlayer2.transform.position = this.transform.position;
}
else
{
camPlayer2.transform.position = new Vector3(Mathf.Clamp(trackPlayer2.transform.position.x, minX, maxX), Mathf.Clamp(trackPlayer2.transform.position.y, minY, maxY), camPlayer2.transform.position.z);
}
if((trackPlayer3 == null))
{
camPlayer3.transform.position = this.transform.position;
}
else
{
camPlayer3.transform.position = new Vector3(Mathf.Clamp(trackPlayer3.transform.position.x, minX, maxX), Mathf.Clamp(trackPlayer3.transform.position.y, minY, maxY), camPlayer3.transform.position.z);
}
if((trackPlayer4 == null))
{
camPlayer4.transform.position = this.transform.position;
}
else
{
camPlayer4.transform.position = new Vector3(Mathf.Clamp(trackPlayer4.transform.position.x, minX, maxX), Mathf.Clamp(trackPlayer4.transform.position.y, minY, maxY), camPlayer4.transform.position.z);
}
UPDATE
Ok so using the code in this question(link), I have managed to get the boundaries working in the top-left camera, when the cameras are 'square' (as in the 4-player layout). However, the other cameras don't track properly at all, and in the rectangular 2-player mode, the top camera still goes too far left-and right. I'm pretty sure I know exactly which line of code is causing the problem... but I don't know what I need to do to fix it...
SpriteRenderer spriteBounds = GameObject.Find("Map").GetComponentInChildren<SpriteRenderer>();
if((trackPlayer1 == null))
{
camPlayer1.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer1.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this
float leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
float rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
float bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
float topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer1.transform.position = new Vector3(Mathf.Clamp(trackPlayer1.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer1.transform.position.y, bottomBound, topBound), camPlayer1.transform.position.z);
}
if((trackPlayer2 == null))
{
camPlayer2.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer2.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this
float leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
float rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
float bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
float topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer2.transform.position = new Vector3(Mathf.Clamp(trackPlayer2.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer2.transform.position.y, topBound, bottomBound), camPlayer2.transform.position.z);
}
if((trackPlayer3 == null))
{
camPlayer3.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer3.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this
float leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
float rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
float bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
float topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer3.transform.position = new Vector3(Mathf.Clamp(trackPlayer3.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer3.transform.position.y, topBound, bottomBound), camPlayer3.transform.position.z);
}
if((trackPlayer4 == null))
{
camPlayer4.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer4.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height; //I guess the problem is here... but how do I fix this
float leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
float rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
float bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
float topBound = (float)(spriteBounds.sprite.bounds.size.y / 2.0f - vertExtent);
camPlayer4.transform.position = new Vector3(Mathf.Clamp(trackPlayer4.transform.position.x, leftBound, rightBound), Mathf.Clamp(trackPlayer4.transform.position.y, topBound, bottomBound), camPlayer4.transform.position.z);
}
Further Edit... I have now managed to get the first camera to track even when it's not a square size with the following code. It still doesn't work for any of the other cameras though
if((trackPlayer1 == null))
{
camPlayer1.transform.position = this.transform.position;
}
else
{
float vertExtent = camPlayer1.orthographicSize;
float horzExtent = vertExtent * (Screen.width * (camPlayer1.rect.width * 2)) / Screen.height; //I guess the problem is here... but how do I fix this??
so, as the main camera is now working as expected regardless of size, I guess that 'all' I need to do now is apply some offsets to leftBound, rightBound, topBound and bottomBound dependent on the rects of the other cams. Any idea how to calculate these offsets?
Your answer
Follow this Question
Related Questions
Animate turning orthographic camera view on and off (like in editor) 0 Answers
Orthographic camera size for 1080p quad @ 1920mx1080m 1 Answer
Trying to use an orthographic camera in interior scenes 0 Answers
How to use ScreenPointToRay for orthographic cameras 1 Answer
How to make WorldToScreenPoint work with an orthographic camera? 1 Answer