- Home /
Smoothly zoom object at point or camera boundaries
How do you smoothly zoom an object at a certain point at runtime? Would you scale/move the object, or change the camera boundaries? In the latter case, how do you make sure the camera contains the point?
Answer by Uzquiano · Apr 29, 2011 at 11:12 AM
Hi,
Maybe you didn't find this post, it could be interesting for you.
But another solution could be to change 'smoothly' the distance in the x-z plane to the target, I mean, if the distance is 10, i would go through 9.9, 9.8...
Your knowlegde of Unity is better than mine, though your question is very interesting, and for me challenging, so I though about the possiblity of take as inspiration the 'ThirdPersonCamera' script that it is in the 3rd person controller provided on the character controller package, especially in the Awake() function. So, if you want to the object is contained, maybe it should be the target of the camera...
function Awake () { if(!cameraTransform && Camera.main) cameraTransform = Camera.main.transform; if(!cameraTransform) { Debug.Log("Please assign a camera to the ThirdPersonCamera script."); enabled = false; }
_target = transform;
if (_target)
{
controller = _target.GetComponent(ThirdPersonController);
}
if (controller)
{
var characterController : CharacterController = _target.collider;
centerOffset = characterController.bounds.center - _target.position;
headOffset = centerOffset;
headOffset.y = characterController.bounds.max.y - _target.position.y;
}
else
Debug.Log("Please assign a target to the camera that has a ThirdPersonController script attached.");
Cut(_target, centerOffset);
}
I hope this could help you.
Cheers
Answer by CHPedersen · Apr 29, 2011 at 11:22 AM
In general, don't move the camera closer to geometry, or geometry closer to the camera. While this does make the object appear bigger, it can also have unintended consequences for other game mechanics, such as collisions, etc. Imagine an FPS where the player has a sniper rifle, and you'd like to zoom whenever the player looks through the scope. If you do that by moving the camera forward, then the player's actual position changes and so is no longer reliable for checking whether you're in sight of enemies, affected by area-effects, etc.
Instead, the physically correct way to go about zooming is to change the camera's field of view. You zoom in by decreasing the field of view and out by increasing it. This makes intuitive sense as well, since if your field of view is smaller, then objects that are inside of it are going to take up more of the resulting screen space, i.e. appear larger.
To change it smoothly, use Mathf.lerp to set it progressively over some amount of frames. If you don't know how, I can cook you up a small example later. :P
Hi, you are totally right about the 'field of view' but how can you assure that your target is inside the view? This is kind of tricky question ;)
If you want to make sure the object to be zoomed at is going to be inside the view frustum after the field of view is changed, you can center it in the camera, i.e. set camera.transform.forward to point directly at the object. An object at the center of the camera's field of view stays in the center after changes to the field of view.
If you're asking for a way to figure out whether an object is going to be visible after the zoom has taken place, this is the same as the check that goes on in view frustum culling. It involves checking the object against the 4 planes of the frustum.
wouldn't you have to move the camera if the point is behind a particular object occluding the cam?
Well, if you want to be able to see behind occluding objects, yes. But then it's not strictly "zoom" any more. Zoo$$anonymous$$g at an occluded point shouldn't cause it to become visible. Think about it - Can you see through walls with a sniper scope?