Answer the question
In order to leave comments, you need to log in
Why do coordinates jump when zooming?
Here is the code for camera movement and zoom, for PC and mobile
private void MoveCamera(){
#if UNITY_EDITOR
if(Input.GetAxis("Mouse ScrollWheel") != 0){
float PrevMapScale = MapScale;
if(Input.GetAxis("Mouse ScrollWheel") > 0){ MapScale = MapScale - 0.5f; }else MapScale = MapScale + 0.5f;
if(MapScale < MapScaleMax) MapScale = MapScaleMax;
if(MapScale > MapScaleMin) MapScale = MapScaleMin;
if(PrevMapScale != MapScale){ MainCamera.orthographicSize = MapScale; }
}
if(Input.GetMouseButtonDown(0)){ CurrentPositionCamera = MainCamera.ScreenToWorldPoint(Input.mousePosition); }
if(Input.GetMouseButton(0)){
Vector3 DifferencePositionCamera = CurrentPositionCamera - MainCamera.ScreenToWorldPoint(Input.mousePosition);
if(DifferencePositionCamera != PrevPositionCamera){
MainCamera.transform.position += DifferencePositionCamera;
PrevPositionCamera = DifferencePositionCamera;
}
}
#else
if(Input.touchCount == 1){
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began){ CurrentPositionCamera = MainCamera.ScreenToWorldPoint(Input.mousePosition); }
else if(touch.phase == TouchPhase.Moved){
Vector3 DifferencePositionCamera = CurrentPositionCamera - MainCamera.ScreenToWorldPoint(Input.mousePosition);
if(DifferencePositionCamera != PrevPositionCamera){
MainCamera.transform.position += DifferencePositionCamera;
PrevPositionCamera = DifferencePositionCamera;
}
}
}
else if(Input.touchCount == 2){
float PrevMapScale = MapScale;
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = (prevTouchDeltaMag - touchDeltaMag) / 100;
MapScale += deltaMagnitudeDiff;
if(MapScale < MapScaleMax) MapScale = MapScaleMax;
if(MapScale > MapScaleMin) MapScale = MapScaleMin;
if(PrevMapScale != MapScale){ MainCamera.orthographicSize = MapScale; }
}
#endif
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question