I am currently using this code to restrict player movement along the horizontal axis, to ensure that the player stays within the camera view. Right now it works for the horizontal axis, but not the vertical axis, for some reason.
In other words, topB and downB values do not seem to be read correctly.
void Update () {
dist = (transform.position.z - cam.transform.position.z);
leftB = cam.ViewportToWorldPoint(new Vector3(0, 0, dist)).x;
rightB = cam.ViewportToWorldPoint(new Vector3(1, 0, dist)).x;
topB = cam.ViewportToWorldPoint(new Vector3(0, 1, dist)).z;
downB = cam.ViewportToWorldPoint(new Vector3(0, 0, dist)).z;
Vector3 pos = transform.position;
pos.x = Mathf.Clamp(transform.position.x, leftB, rightB);
pos.z = Mathf.Clamp(transform.position.z, downB, topB); //Not working well:
// topB is roughly the center of the screen while downB is well beyond the
// bottom of the screen.
transform.position = pos;
}
Why can’t I make the camera just follow the player?
I have two players on screen and a camera which increases the ortographicSize as they get farther away from each other. At a certain point it stops stretching and needs to start restricting player movement instead.
Notes
The game is top down at an angle. I have been looking for help on camera.ViewportToWorldPoint, but I think I am not getting the dist (distance) right, since the camera is rotated at an angle, not straight down on the players (the cubes in the pictures below).