Suppose that I have the three game(cube,Quard,sphere) object. When I click on a particular game object suppose think I have clicked on the cube,the cube object should get zoomed in on the screen.

how can I do that?

How do I center and zoom into my cube game object after selecting the cube? I think it has something to do with the camera but how do I center the view with the camera?

I have used the below code for zooming the object.For one object its working.But when I place different object and apply this code then when i click on one object all the three object is getting zoomed.

Can anyone help me out in solving this issue

Here is the code

  function OnMouseDown()
function OrthoZoom(size : float) {
 var cam = Camera.main;

 while (cam.orthographicSize != size) {
     cam.orthographicSize = Mathf.MoveTowards(cam.orthographicSize, size, speed * Time.deltaTime);


  • \$\begingroup\$ Although it's not exactly what you want, you can have a look at this script which has the ability to zoom into the point between your moving fingers. savalishunitytutorials.com/touchmovementjavascript.js \$\endgroup\$
    – Savlon
    Aug 26, 2015 at 2:48
  • \$\begingroup\$ add a selection identifier script, that can identify the selected object and then make the camera script check for the select objected and some on it. General controllers are the correct practice to reuse scripts. \$\endgroup\$ Jun 26, 2016 at 10:20

1 Answer 1


One way to solve this, is by parenting the object to the camera, when the object receives the OnMouseDown signal.

This could look similar to this:

function OnMouseDown()
    Camera.main.transform.parent = this.transform;

    // Optionally, set some flag and smoothly move towards a Vector, same with the size
    Camera.main.transform.position = someVector;
    Camera.main.transform.orthographicSize = sizeBasedOnCurrentObject();

Note, that the pivot of the object itself, should be centered for this to be "truly" centered.

You will also have to calculate someVector, based on the current Camera's transforms. E.g. if the Camera's viewing ray is equal to (0,0,1), someVector should be (0,0,z), where z should be great enough to not lie inside the centered object.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .