Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 13, 2019 at 10:42 AM by Capricornum for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Capricornum · Jun 13, 2019 at 05:51 AM · raycastingmousedraggetmousebutton

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Capricornum · Jun 13, 2019 at 10:41 AM 1
Share

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

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object appear only when I click and not when I drag 2 Answers

Multiple Camera.main.ScreenPointToRay 1 Answer

How to destroy a gameObject when hit by Raycast? 4 Answers

Shotlock System 1 Answer

Ground Hugging Vehicle 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges