- Home /
The question is answered, right answer was accepted
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.
Answer by Kennai · Jun 13, 2019 at 08:37 AM
Wanna know why it dosnt trigger GetMouseButtonUp? because of that piece of code :D
if (currentlySelectedTransform.position == targetDragPos)
return;
Oh hell, am I actually returning out of the whole update loop with that piece of code? And the if(Input.Get$$anonymous$$ouseButtonUp(0)) never actually gets called?
Somehow I thought I was only breaking out of the if statement. But that doesn't make any sense at all does it?
Thank you!!
Follow this Question
Related Questions
How to make an object appear only when I click and not when I drag 2 Answers
Will I need to use raycasts for my 2D game? 1 Answer
transform.forward doesn't correspond to rotation 1 Answer
Physics Raycast work fine but don't work after build game. 0 Answers
Raycasting not working on a procedural mesh with a mesh collider. 1 Answer