user8810083
user8810083

Reputation: 617

Automatically calculating new position of camera after we increase our chessboard size but want it still to stay in shot

Say my camera is rotated around the X axis 60 degrees and looking down on a 9x9 block chess board. As we adjust board size, I want to zoom out the camera. Say for arguments sake the camera's position is (4,20,-7) and like this the whole board is visible and taking up the full screen.

If I adjust my board size to say 11x11 blocks I will now need to zoom out the camera. Say I want to maintain the same 60 degree angle and want the board to fill as much of the screen as it did before. What should the camera's new position be and how do you calculate it?

The X part is easy since you simple give the camera the same X position as the middle of the board. I'm not sure about how to calculate the new Y and Z positions though.

Any advice appreciated. Thanks.

edit: and if i wanted to change the angle of the camera as well as zoom out, is that possible to calculate? this is less important since i'll probably stick with the same angle, but i'm interested to know the maths behind it anyway.

Upvotes: 0

Views: 43

Answers (1)

MyBug18
MyBug18

Reputation: 2240

Transform.Translate() method will move the transform according to the rotation. So you don't have to worry about the direction where your camera is looking at, just

yourCamera.transform.Translate(Vector3.forward * moveAmount);

will move your camera forward, which means zoom in. If you want to zoom out, just change the sign of the value to minus.

When I didn't know this, I used Mathf.Sin() and Mathf.Cos() to calculate each y and z world coordinates, which sucks.

Upvotes: 0

Related Questions