- Home /
Dynamic Orthagraphic Camera Zoom
So im working on a 2d 2player student project, we are trying to have a camera that tracks both players. Atm ive worked out how to keep the cam in the middle of the players but as for the cam distance its been a little tricky trying to work it out. the code isent the most elegant so far but it does the job
var player1 : Transform; var player2 : Transform;
function Update () { var sum1 : float; var sum2 : float; var sum3 : float; var sum4 : float;
sum1 = player1.position.x + player2.position.x;
sum2 = sum1 / 2;
transform.position.x = sum2;
sum3 = player1.position.z + player2.position.z;
sum4 = sum3 / 2;
transform.position.z = sum4;
}
If someone could point me in the right direction it would be much appreciated
Answer by J8Ptt · Feb 04, 2012 at 10:58 PM
One of the ways you can use to do that can be using the distance between both players to calculate the camera's y position.
I just added the following to your code:
transform.position.y = Vector3.Distance(player1.position, player2.position) * 0.75;
and it looked like it may be what you need (the "* 0.75" is a value that you'll probably wanna tweak to your project).
Hope it helps.
EDIT: Sorry, didn't notice you have an orthographic camera, but the same applies: you just map the value to the orthographicSize parameter.
camera.orthographicSize = Vector3.Distance(player1.position, player2.position) * 0.75;
Your answer
Follow this Question
Related Questions
2D camera zoom in comparison to the height of target 2 Answers
Dynamic shadows in orthographic camera. 2 Answers
Ortho camera zoom to mouse point 4 Answers
How do I stop objects from ghosting on screen? 1 Answer
Set camera to specify x,y in pixels? 2d 0 Answers