Hi guys,
I am implementing a zoom system in the game to be a bit google-maps like. i.e. if you zoom in, the worldspace is being locked in position under the mouse pointer, exactly like google maps does.
I think i am fairly close to a script that handles that, but somehow something still fails in the camera position. The zoom itself back and forward with the mouse wheel works smoothly, but this “lock on pointer” effect keeps on failing. This is how i did so far, i am sooo stupid it’s some small thing related to the coordinates spaces, but somehow i can’t nail it.
//zoom in the map
public void ZoomIn(){
zoomIn = true;
oldNormalizedScale = (targetOrtho - orthographicSizeMin) / (orthographicSizeMax- orthographicSizeMin);
targetOrtho = Mathf.Clamp(mainCamera.orthographicSize-zoomSpeed, orthographicSizeMin, orthographicSizeMax );
normalizedScale = (targetOrtho - orthographicSizeMin) / (orthographicSizeMax- orthographicSizeMin);
minX = (1f-normalizedScale)*lowerLeft.x;
minY = (1f-normalizedScale)*lowerLeft.y;
maxX = (1f-normalizedScale)*upperRight.x;
maxY = (1f-normalizedScale)*upperRight.y;
maxLowerLeft = new Vector2 (minX, minY);
maxUpperRight = new Vector2 (maxX, maxY);
if (normalizedScale > 0) {
Vector3 mousePos = mainCamera.ScreenToWorldPoint (Input.mousePosition);
float scaleFactor = (oldNormalizedScale/normalizedScale);
//calculate the difference between the current zoom level mouse-cameraCenter vector and the same vector scaled according to the new zoom level
offsetCamera = (mousePos - mainCamera.transform.position)-(mousePos - mainCamera.transform.position) / scaleFactor;
moveCameraTo = new Vector3 (Mathf.Clamp (mainCamera.transform.position.x + offsetCamera.x, maxLowerLeft.x, maxUpperRight.x), Mathf.Clamp (mainCamera.transform.position.y + offsetCamera.y, maxLowerLeft.y, maxUpperRight.y), mainCamera.transform.position.z);
mainCamera.transform.position = moveCameraTo;
}
}
It just doesn’t work, the camera moves a bit, probably also in the right direction, but somehow not as it should (i.e. the map still moves under the pointer and is not locked there).
Any help would be absolutely welcome!
Cheers,
H