0

In my 2D game, I need to drag an object and move it to another object. I detect collisions between the two objects by adding a rigidbody to the moving object and a collider to both. I also have the moving object in a sorting layer in front of the other object:

void OnMouseDown() {
        print( transform.name + "Clicked");
        screenPoint = Camera.main.WorldToScreenPoint(transform.position);
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }

    void OnMouseDrag() {
        Vector3 mousePoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(mousePoint) + offset;
        transform.position = mousePosition;
    }

    void OnTriggerEnter2D(Collider2D other) {

        isInsideObject = true;
    }

    void OnTriggerExit2D(Collider2D other) {

        isInsideObject = false;
    }

The code works fine, but once the moving object collides with the other object and I release the mouse, I can't click on it and drag it again. this line doesn't get executed:

print( transform.name + " Clicked");

EDIT: This question is not an "exact duplicate" of the other question. And your answer is not applicable to my case.

Abdo23
  • 77
  • 7
  • This question is not an "exact duplicate" of the other question. And your answer is not applicable to my case. – Abdo23 Feb 09 '18 at 14:16
  • It shows you how to detect click on an Object which different from what you have in your question that is not currently moving.... – Programmer Feb 09 '18 at 14:47
  • What?! I don't understand what you just said. Again I know how to detect a click on an object, I've already done it, the problem is when 2 objects are colliding with each other. If I remove the collider from the still objects the detection happens fine. Do you even get this or are you just trying to farm votes for your other answer? – Abdo23 Feb 09 '18 at 15:30
  • I read this question properly before closing it and just read it again just to be sure but don't see what I did wrong. It says *"Can't click on an object when.."* in the title. Again, in the body, it says *"I can't click on it and drag it again"*. The duplicate I placed shows a *proper* way to detect click on objects or drag any type of GameObject. The way to do this are different in your question and in the duplicate. If you tried it and the-same problem still persist, I would have been willing to help more. If I want to farm rep, I would have copied my answer and pasted it here. – Programmer Feb 09 '18 at 16:54
  • So, just because two questions contain similar phrases then they are automatically the exact same questions and therefore they should have the same answer? I've provided code and asked why my code doesn't work in a "certain" situation. Your solution was to force me to another answer with completely different code even though my code works but apparently missing something without even pointing out what my code's problem might be, and preventing anyone else from providing a proper answer in the process. It's amazing how someone with this way of thinking is allowed to be in charge. – Abdo23 Feb 09 '18 at 17:08

0 Answers0