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.