Unity32 (2D!) Camera problem....

Pleng

Custom Title
OP
Member
Joined
Sep 14, 2011
Messages
2,439
Trophies
2
XP
2,810
Country
Thailand
Well as usual my question had no response from Stack Overflow, so perhaps somebody here can help fill the void...


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, for example...
2cams.png
4cams.png


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.

req.png


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...

Code:
SpriteRenderer spriteBounds = GameObject.Find("Map").GetComponentInChildren<SpriteRenderer>();
float leftBound =0;
float rightBound =0;
float bottomBound =0;
float topBound = 0;
 
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??
 
    leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
    rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
    bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
    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??
 
    leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
    rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
    bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
    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??
 
    leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
    rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
    bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
    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??
 
    leftBound = (float)(horzExtent - spriteBounds.sprite.bounds.size.x / 2.0f);
    rightBound = (float)(spriteBounds.sprite.bounds.size.x / 2.0f - horzExtent);
    bottomBound = (float)(vertExtent - spriteBounds.sprite.bounds.size.y / 2.0f);
    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);
}

So I'm pretty sure that I need to be checking against the cameras size and relative position on the screen, but I am lost as to exactly what I need to be doing.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • Veho @ Veho:
    No it isn't.
  • JuanMena @ JuanMena:
    Basically means you don't get the attention you want from mommy, daddy and real people, so you substitute real human interaction with online interactions. And you get depressed because you don't get attention.
  • Xdqwerty65 @ Xdqwerty65:
    But I get attention from my family
  • Xdqwerty65 @ Xdqwerty65:
    And i sometimes get attention from real people
  • Xdqwerty65 @ Xdqwerty65:
    I mean
  • Xdqwerty65 @ Xdqwerty65:
    People who arent from my family
  • JuanMena @ JuanMena:
    Don't worry, we're all a big family on the TEMP... and then there's Veho.
  • Veho @ Veho:
    We're all a big happy dysfunctional family, I'm the grumpy killjoy, Juan is the drunken
    uncle from Florida.
  • Veho @ Veho:
    Ken is Ken.
  • JuanMena @ JuanMena:
    And like y'all uncle, I need you to send nudes.
  • JuanMena @ JuanMena:
    Anyway, gonna be a real uncle now, taking niece to play outside with doggos.
    +1
  • a_username_that_is_cool @ a_username_that_is_cool:
    Working clock
  • a_username_that_is_cool @ a_username_that_is_cool:
    I don't have one
  • Xdqwerty65 @ Xdqwerty65:
    I have one
  • Xdqwerty65 @ Xdqwerty65:
    But i'm gonna tell you
  • Xdqwerty65 @ Xdqwerty65:
    If You arent watching the super Mario Bros Super show...
  • Xdqwerty65 @ Xdqwerty65:
    You go to hell before you die You gonna turn into a goomba
  • TheBakingGamer0424 @ TheBakingGamer0424:
    @Xdqwerty65 I just play some L4D2, I have some goofy ahh mods, so my teams end up looking like these
    myl4d1crew-png.16011
    myl4d2crew-png.16012
  • Xdqwerty65 @ Xdqwerty65:
    Ok but why did You mention me
  • TheBakingGamer0424 @ TheBakingGamer0424:
    and the zombies were replaced with staff bots from fnaf security breach
  • Xdqwerty65 @ Xdqwerty65:
    Oh yeah the feeling depressed thing
  • Xdqwerty65 @ Xdqwerty65:
    Gonna download warioware inc
  • Xdqwerty65 @ Xdqwerty65:
    To see if i feel better
  • TheBakingGamer0424 @ TheBakingGamer0424:
    yeah i mentioned you bc i made that message shortly after the depressed message, but needed to link the art, so i had to make it and post in on the Wario Forums first
    TheBakingGamer0424 @ TheBakingGamer0424: yeah i mentioned you bc i made that message shortly after the depressed message, but needed to...