• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Game Crashes if Mouse Clicked while Loading Scene 0 Answers

Small issue with code, Help needed! 0 Answers

Left Mouse doesn't register click when keyboard button is pressed 1 Answer

Click an object and show text 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges