Z
Z
Zefirot2021-07-12 20:28:56
Unity
Zefirot, 2021-07-12 20:28:56

How to move the camera with the mouse finger+zoom?

Recently I did all this and it worked perfectly, until I connected the mesh, the fact is that I had a 50x50 2D box Collider object and I rotated it as I wanted, all the objects were actually on it, zoomed in / out, moved with mouse and finger...

spoiler
private void ZoomEditPanelMap(){
        #if UNITY_EDITOR
        if(Input.GetAxis("Mouse ScrollWheel") != 0){
            PossibleMove = false;
            float PrevMapScale = MapScale;
            if(Input.GetAxis("Mouse ScrollWheel") > 0) MapScale = MapScale + 0.05f; else MapScale = MapScale - 0.05f;
            if(MapScale > MapScaleMax) MapScale = MapScaleMax;
            if(MapScale < MapScaleMin) MapScale = MapScaleMin;
            if(PrevMapScale != MapScale){
                Vector3 Pos = ObjectControlBoard.transform.localPosition;
                ObjectControlBoard.transform.localPosition = new Vector3((Pos.x / PrevMapScale) * MapScale, (Pos.y / PrevMapScale) * MapScale, 1f);
                ObjectControlBoard.transform.localScale = new Vector3(MapScale, MapScale, 1f);
                }
            }else PossibleMove = true;
        #else
        if(Input.touchCount == 2){
            PossibleMove = false;
            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) / 500;
            float PrevMapZoom = MapZoomTouch.x;
            if(MapZoomTouch.x <= MapScaleMin & deltaMagnitudeDiff > 0) deltaMagnitudeDiff = 0;
            if(MapZoomTouch.x >= MapScaleMax & deltaMagnitudeDiff < 0) deltaMagnitudeDiff = 0;
            MapZoomTouch = new Vector3(MapZoomTouch.x -= deltaMagnitudeDiff, MapZoomTouch.y -= deltaMagnitudeDiff, 1);
            if(MapZoomTouch.x > MapScaleMax) MapZoomTouch.x = MapScaleMax;
            if(MapZoomTouch.x < MapScaleMin) MapZoomTouch.x = MapScaleMin;
            if(MapZoomTouch.y > MapScaleMax) MapZoomTouch.y = MapScaleMax;
            if(MapZoomTouch.y < MapScaleMin) MapZoomTouch.y = MapScaleMin;
            MapZoomTouch.x = MapZoomTouch.y;
            MapScale = MapZoomTouch.x;
            if(PrevMapZoom != MapZoomTouch.x){
                Vector3 Pos = ObjectControlBoard.transform.localPosition;
                ObjectControlBoard.transform.localPosition = new Vector3((Pos.x / PrevMapZoom) * MapZoomTouch.x, (Pos.y / PrevMapZoom) * MapZoomTouch.x, 1f);
                ObjectControlBoard.transform.localScale = MapZoomTouch;
                }
            }else PossibleMove = true;
        #endif
        }
    public void OnDrag(PointerEventData eventData){
        if(PossibleMove){ ObjectControlBoard.transform.Translate(eventData.delta / (Screen.height / 10)); }
        }

Everything worked perfectly both on the computer and on the phone....

But when I started working with meshes, when this object moved, I noticed that the mesh mask was moving, the mesh targets were shifting, etc. , in general, you need to change the rhetoric ....

The conclusion is actually obvious - you need to move the camera itself and zoom in / out ...
Tell me ready-made solutions to this problem, I think there are a lot of them, but for now I'm digging and I can't find a normal one ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
ReyGrau, 2021-07-13
@Zefirot

Searched in a couple of seconds Look

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question