0

I have a script which limits camera movement to within a defined range. Unfortunately, it only works on the first camera, and there are up to four orthographic cameras in my game.

I have been unable to adapt the script to work with multiple cameras, so I figured that the next best this would be to translate the points from the four corners from the first camera's viewport to world position, then from the world position back to the viewport of cameras 2, 3 and 4. However this doesn't appear to be working.

    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 * (camPlayer1.rect.width * 2)) / 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
    {

        leftBound = camPlayer1.ViewportToWorldPoint(new Vector3(leftBound, 0, 0)).x;
        rightBound = camPlayer1.ViewportToWorldPoint(new Vector3(rightBound, 0, 0)).x;
        bottomBound = camPlayer1.ViewportToWorldPoint(new Vector3(0, bottomBound, 0)).y;
        topBound = camPlayer1.ViewportToWorldPoint(new Vector3(0, topBound, 0)).y;

        leftBound = camPlayer2.WorldToViewportPoint(new Vector3(leftBound, 0, 0)).x;
        rightBound = camPlayer2.WorldToViewportPoint(new Vector3(rightBound, 0, 0)).x;
        bottomBound = camPlayer2.WorldToViewportPoint(new Vector3(0, bottomBound, 0)).y;
        topBound = camPlayer2.WorldToViewportPoint(new Vector3(0, topBound, 0)).y;

        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);
    }
LairdPleng
  • 948
  • 3
  • 9
  • 28
  • 1
    Back to your first problem... your script works only on the first camera, but not on the others. The script was attached to every camera or was it a global script? – Gounemond Jan 08 '15 at 09:40
  • this is a global script. camPlayer1 - camPlayer4 and trackPlayer1 - trackPlayer4 are passed in by reference. The cameras track the players ok, but once I start trying to clamp the positions, it doesn't work. I posted the original problem [here](http://stackoverflow.com/questions/27752833/unity-camera-to-centre-on-player-but-never-exceed-map-bounds) – LairdPleng Jan 08 '15 at 13:59

0 Answers0