Input.GetMouseButtonUp not detected after drag

Dear Community,

I have a capsule that I want to drag over a floor. My first approach was to have a script on the capsule answering to the OnMouseDrag Message. That worked fine. However it lags the OnDragEnd functionality.

So I wanted to attempt the same with a MouseInputController that shoots rays. I figured, that’s what Unity is doing behind the scenes anyhow. My script detects a mouse click, sets a flag (mouse clicked) to true, allows dragging while the flag is true and sets the flag to false again, when it detects the GetMouseButtonUp.
Sometimes this works fine. But sometimes after wildly dragging the capsule around, the GetMouseButtonUp is not detected. And while I did indeed release the mouse, I can still drag the capsule around as if I hadn’t.

Here is my script.

using UnityEngine;

public class MouseInputManager : MonoBehaviour
{
    //for Raycasting
    public LayerMask movablesLayer;
    public LayerMask groundLayer;
    Camera cam;
    Ray ray;
    RaycastHit hit;

    //for checking what the mouse does
    bool mouseDown = false;
    string hAxis = "Horizontal";
    string vAxis = "Vertical";

    //keeping track of the draggable transform and the target position to drag to
    Transform currentlySelectedTransform;
    Vector3 targetDragPos;

    private void Awake()
    {
        cam = Camera.main;
    }

    private void Update()
    {
        //Mouse Click down: Drag begin
        if (Input.GetMouseButtonDown(0))
        {
            ray = cam.ScreenPointToRay(Input.mousePosition);

            //checking for objects on the movables layer. Thats the capsule.
            if (Physics.Raycast(ray, out hit, 100f, movablesLayer))
            {
                mouseDown = true;
                currentlySelectedTransform = hit.collider.transform;
            }
        }

        //Mouse Drag
        if (mouseDown)
        {
            //Axis are set to Mouse Input in the Project Settings
            if (Input.GetAxis(hAxis) > 0 || Input.GetAxis(vAxis) > 0 || Input.GetAxis(hAxis) < 0 || Input.GetAxis(vAxis) < 0)
            {
                ray = cam.ScreenPointToRay(Input.mousePosition);

                //checking for objects on the ground layer. Thats the quad.
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, groundLayer))
                {
                    targetDragPos = GridClamp(hit.point);

                    if (currentlySelectedTransform.position == targetDragPos)
                        return;
                    currentlySelectedTransform.position = targetDragPos;
                }
            }
        }

        //Mouse Click Up: Drag End
        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("Mouse Button Up detected.");
            mouseDown = false;
        }
    }

    Vector3 GridClamp(Vector3 pos)
    {
        float x = Mathf.Round(pos.x);
        float y = 0;
        float z = Mathf.Round(pos.z / 0.866f) * 0.866f;

        return new Vector3(x, y, z);
    }
}

I’d be grateful for any help.

Otherwise I will attempt the third approach using the EventSystems Drag interfaces. Because those include a DragEnd. But I would really like to understand what I am doing wrong.

Thank you.

Wanna know why it dosnt trigger GetMouseButtonUp?
because of that piece of code :smiley:

if (currentlySelectedTransform.position == targetDragPos)
  return;