- Home /
How can I limit the player's camera to move to the edge of the map centering it on the camera?
I made a Script in Unity that moves my camera all over the map and can zoom in and out.
Camera starts rotated.x at 60° with a distance of 30 (minimum 15, maximum 60). I'm using:
cameraLimiteX = (mapPlayableSize.localScale.x / 2);
cameraLimitXn = -1 * ((mapPlayableSize.localScale.x / 2);
...to determine as much as she can "off the map". For now, as it has no rotation, X and -X work perfectly.
Here comes my question regarding the Y, how should I calculate the Y limits? If I do like this:
cameraLimiteY = (mapPlayableSize.localScale.z / 2);
cameraLimiteYn = - 1 * ((mapPlaySize.localScale.z / 2));
it works perfectly with the 90º camera
But with the camera at 60º no.
So I think I should use the camera angle + camera distance from the ground to determine these limits.
Then I did something like this:
var xSin = Mathf.Sin(transform.rotation.x);
var xAsin = Mathf.Asin(transform.rotation.x);
var xTan = Mathf.Tan(transform.rotation.x);
var xHeight = transform.position.y;
cameraLimiteY = (mapPlaySize.localScale.z / 2) - (xTan * xHeight);
cameraLimitYn = -1*((mapPlayableSize.localScale.z / 2) + (xTan * xHeight));
seems to be what I need: even changing the height, it centers on the edge of the map. HOWEVER, as I want to be able to change the angle, if I do, the limits don't fit perfectly... I would like to know if it is possible to make these limits based on angle/height with some mathematical function/expression...
I've been hitting my head here, I even made this illustration to help me in the process, I'm still not able to move forward.