Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by SufianDira · Jul 11, 2020 at 06:04 PM · mousemouseclickmouse-drag

Objects not Detecting Mouse Clicks Reliably (OnMouseDown())

I have set up the following script to let the player drag objects around my physics-based game. Sometimes it works flawlessly but half of the time it doesn't detect the mouse at all.

The same script is applied to multiple objects and they all have a rigidbody 2d and a box collider 2d, some instances of a preface might work while the other doesn't in the same scene.

I'm using Cinemachine with one virtual orthographic 2d camera.


 public float mouseSpeed = 35f;
 public float movementSmoothing = 2f;
 Vector2 mousePos;
 Vector2 mouseStartOffset;

 Rigidbody2D rb;

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 void OnMouseDown()
 {
     mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     mouseStartOffset = new Vector2(mousePos.x - transform.position.x, mousePos.y - transform.position.y);
     Debug.Log("The Mouse is Down!");
 }

 void OnMouseDrag()
 {
     mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     rb.MovePosition(Vector2.Lerp(rb.position, Vector2.MoveTowards(rb.position, mousePos - mouseStartOffset, mouseSpeed * Time.deltaTime), movementSmoothing));
     Debug.Log("Dragging!");
 }
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

· Add your reply
  • Sort: 
avatar image
0

Answer by SufianDira · Jul 11, 2020 at 07:05 PM

I tried the following approaches:

1- Deleting all the objects and prefabs and remaking them.
2- Changing the camera and camera settings.
3- Changing the order in layer and z-axis of all objects.
4- Inserting Debug.Log() in the script to make sure the action isn't registered.

What I found out;
1- Objects dragging works fine only after physically interacting with them, i.g. bushing them with another object.

2- By Adding:

 void Update()
     {
         if (Input.GetKeyDown(KeyCode.A))
             rb.AddForce(new Vector2(0f, 100f));
     }

Dragging will work fine for some time after pressing "A", but it will stop working after around 10 seconds.

But changing the rigid body 2D sleeping mode to "never Sleep" or changing the collision detection to "Continuous" won't fix the issue.

Still looking for a fix ...

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

212 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 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

Need help with doing something in my game 0 Answers

Event on mouse up 2 Answers

Detect mouse press and release 3 Answers

(bluetooth mouse)right mouse button in android 0 Answers

OnMouseDrag() stopped working after few clicks 0 Answers


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