- Home /
2D camera zoom in comparison to the height of target
I have a 2d vehicle moving along a 2d surface, and the camera is orthographic, and follows the vehicle side on as it moves. I need the camera to zoom in and out a bit when the vehicle jumps over a ramp, so the higher it goes the further out the camera zooms, or rather changes orthographic size, and then reverts back when the vehicle lands again, with smoothing, is this possible?
cheers - monster.
Deter$$anonymous$$e the height your car is off the ground. You can do this with a raycast or another method depending on how your game is built.
Deter$$anonymous$$e how high you expect your car to jump and the $$anonymous$$ and max size of the ortho.
Ex:
carHeight = 0 to 10
orthoSize = 10 to 50
orthoRange = 40 (50 $$anonymous$$us 10)
ratio = 4 (orthoRange/maxCarHeight)
orthoSize = carHeight * 4 + 10;
thanks - this makes sense, but im afraid i lack the scripting knowledge to fully put this together (still a newbie) :p
im using a physics raycast to deter$$anonymous$$e the height so how would i put this together.. im just trying and failing again and again, im using JS if you could help out again :) cheers for the reply though!
right ive worked out the body of the script, but how do I call in the 'carHeight' from the raycast on the gameobject, into the script on the camera? anybody?
Answer by Berenger · Feb 14, 2012 at 04:58 PM
It depends how you're making your camera moves, but I guess it's a child of the vehicle. So, when it jumps, the camera jumps to, changing it's heights, and you want the size to change as well. This is what I would do in a script attached to the camera :
var : relativeHeight = (transform.position.y - minHeight) / (maxHeight-minHeight);
camera.orthographicSize = Mathf.Lerp( minSize, maxSize, relativeHeight );
If you can find the correct values for min and max, it should work.
thanks alot - works a treat, although dont have the camera as a child of the vehicle otherwise it rotates with the car too, but the script works fine, just need to tweak the values.
Answer by ratboy · Feb 14, 2012 at 11:12 PM
right ive worked out the body of the script, but how do I call in the 'carHeight' from the raycast on the gameobject, into the script on the camera? anybody?
Your answer
Follow this Question
Related Questions
Dynamic Orthagraphic Camera Zoom 1 Answer
Orthographic camera movement clamping 1 Answer
How to make a smash bros-like camera 3 Answers
Camera too zoomed in in built game 1 Answer