Hi, I’d like to ask you guys (or girls) for a little help.
I’m currently trying to implement a zoom-in/zoom-out control for an orthographic camera. So I’m modifying its size but I want to set some limits. I used the “Mathf.Clamp” function but for some reason the value get set to 0 instead of the min/max value. Here’s the code:
public Camera previewCamera;
public float zoomSpeed = 10.0F;
public float maxZoom = 0.01F;
public float minZoom = 0.5F;
private float cameraZoom;
void Start () {
cameraZoom = previewCamera.orthographicSize;
Debug.Log(cameraZoom);
}
void OnMouseOver() {
CameraZoom(Input.GetAxis("Mouse ScrollWheel"));
}
private void CameraZoom (float mouseWheelAxis) {
cameraZoom -= mouseWheelAxis / zoomSpeed;
Debug.Log(cameraZoom);
cameraZoom = Mathf.Clamp(cameraZoom, maxZoom, minZoom);
Debug.Log(cameraZoom);
previewCamera.orthographicSize = cameraZoom;
}
The very first Debug.Log displays “0.3”, which is the default size value in the inspector. The second one too but the third one just after the clamp displays “0”. It’s also weird that the value is modified in the first place since its default value is set between the min & max values.
“maxZoom” being set to the Clamp min parameter is not a mistake (and vice-versa), it might be confusing here but since zooming-in makes the size smaller I preferred to name it that way.
If someone had a solution to this it would be really helpful.
Thank you.
If you try logging the maxZoom and minZoom values do you get the expected values?
– emalbI've just tried that: they were both at 0. Thanks for the advice, now I know the problem has to do with those values and not the clamp.
– thagiwara