I am doing my first unity2d game and im in need of a little optimization for my camera.

I have a camera which could pan and zoom with the speed i have defined. The problem with that is the speed i defined for panning will be too fast when zoomed in and soo slow when zoomed out. Maybe it could be achieved by having varying speed with zoom level. I dont want that.

Instead, i would like to have the camera used in Clash of Clans (i have seen it only in that).

The camera pans/zooms async with the world. That is, at any zoom level, if we placed our finger on an object X and started moving, the object X will be right below our fingers all the way. That means the world moves relatively.

Its the same with the zooming. If we placed two fingers on object X & on object Y and started 'zooming' in or out, Those two objects will be there right below the fingers all the way.

That is what i would like.

For panning, i tried this and failed : (Unity C#)

Touch t = Input.getTouch(0); //when touchcount = 1 only

Vector2 prev_touch_position = t.position - t.deltaPosition;

Vector3 resultant_unitvector = (prev_touch_position - t.position) / (prev_touch_position - t.position).magnitude;

float resultant_magnitude = Info.half_height / camera.orthographicSize * (prev_touch_position - t.position).magnitude; //Info.half_height is calculated during change in orthographic size and is a static variable.

camera.transform.position += resultant_unitvector * resultant_magnitude;

The above code doesnt work as expected on debugging.

It would be a great help for the community if someone could solve this cuz that is some pretty good camera :) (Include damping after touch release if you could ? :) ) Im just a student by the way :)

Thanks in advance :)


So I have found the solution at last :P the problem was the position of the Touches were in pixels and i took them as for screen coordinates. So to fix it,

float resultant_magnitude = Info.half_height / camera.orthographicSize * (prev_touch_position - t.position).magnitude;

replace the above by this :

float resultant_magnitude = Info.half_height / (Screen.height / 2) * (prev_touch_position - t.position).magnitude;

This helped :)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.