Z
Z
Zefirot2021-09-28 11:06:48
Unity
Zefirot, 2021-09-28 11:06:48

What causes sticking?

Making a simple camera move
void Update(){
            MoveCamera();
        }
public bool IsPointerOverUIObject() {
        PointerEventData ep = new PointerEventData(EventSystem.current);
        ep.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(ep, results);
        return results.Count > 0;
        }
    private void MoveCamera(){
                if(!IsPointerOverUIObject()){
                    if(Input.GetMouseButtonDown(0)){ CurrentPositionCamera = MainCamera.ScreenToWorldPoint(Input.mousePosition); }
                    if(Input.GetMouseButton(0)){ VerifyBoundCamera(); }else{ CameraMove = false; }
                    if(Input.GetMouseButtonUp(0)){ CameraMove = false; }
                    }else{ CameraMove = false; CurrentPositionCamera = MainCamera.ScreenToWorldPoint(Input.mousePosition); }
                }
        }
    private void VerifyBoundCamera(){
        PosInputMouse = MainCamera.ScreenToWorldPoint(Input.mousePosition);
        DifferencePositionCamera = CurrentPositionCamera - PosInputMouse;
        if(MainCamera.transform.position.y + DifferencePositionCamera.y > MaxY){
            DifferencePositionCamera.y = 0f;
            CurrentPositionCamera = new Vector3(CurrentPositionCamera.x, PosInputMouse.y, -1f);
            }else if(MainCamera.transform.position.y + DifferencePositionCamera.y < MinY){
                DifferencePositionCamera.y = 0f;
                CurrentPositionCamera = new Vector3(CurrentPositionCamera.x, PosInputMouse.y, -1f);
                }
        if(MainCamera.transform.position.x + DifferencePositionCamera.x > MaxX){
            DifferencePositionCamera.x = 0f;
            CurrentPositionCamera = new Vector3(PosInputMouse.x, CurrentPositionCamera.y, -1f);
            }else if(MainCamera.transform.position.x + DifferencePositionCamera.x < MinX){
                DifferencePositionCamera.x = 0f;
                CurrentPositionCamera = new Vector3(PosInputMouse.x, CurrentPositionCamera.y, -1f);
                }
        if(DifferencePositionCamera != PrevPositionCamera){
            MainCamera.transform.position += DifferencePositionCamera;
            PrevPositionCamera = DifferencePositionCamera;
            CameraMove = true;
            }else CameraMove = false;
        }

Everything moves as expected, but sometimes sticking occurs. That is, this is how the movement occurs when the left mouse button is held down, and stops when it is released, and it is so rare, but it happens that the button is released, but the movement behind the mouse remains until it is pressed again. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BFGunner, 2021-10-03
@Zefirot

Didn't check the entire script, but noticed the following:
1. Click-once checks are not next to each other. Those. after a single press test, a long press test is started.
2. When checking for a long press, a method is launched that, until it has fully worked out, will continue to move
3. The gag can be caused by this. Those. there is an invalid boolean condition in the method fired on the hit test that keeps moving.
Solution:
1. Put Down and Up checks next to each other
2. Comment out Down and Up and check how
getButton works 3. If getButton works fine when commenting Up and Down, look for reasons

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question