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?


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 = - _target.position;
        headOffset = centerOffset;
        headOffset.y = characterController.bounds.max.y - _target.position.y;
        Debug.Log("Please assign a target to the camera that has a ThirdPersonController script attached.");

    Cut(_target, centerOffset);

I hope this could help you.


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