Z
Z
Zefirot2021-07-16 15:56:04
Unity
Zefirot, 2021-07-16 15:56:04

Why do coordinates jump when zooming?

Here is the code for camera movement and zoom, for PC and mobile

spoiler
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
        }

on the PC (in the editor) everything works perfectly, on the phone if you just move the camera, then it’s normal, but when I start to change the zoom, the camera coordinates start jumping, I’ve been spinning for an hour and I can’t understand what’s the matter then?
Tell...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question