- Home /
translate a point between two cameras
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);
}
I don't see how that is going to help with limiting the second cameras movement range. Why don't you post a question about your movement script? I am sure we can get it to work somehow.
@GameVortex - could you explain or link to somewhere that explains the theory of why translating the boundaries between the cameras won't work? Surely if I have four points that are understood and accepted by camera 1, translating that point to the world, then back to the viewport of the other cameras should give me the correct points to clamp the camera's movements?
Your answer
Follow this Question
Related Questions
Keeping the camera in bounds 1 Answer
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