- Home /
Drag and drop collisions
Hi, I have a problem with the OnTriggerEnter function. I have a few sprites in my scene with a drag and drop script attached to them. The script is taken from the internet and works fine. When a sprite is dragged and collides with a specified sprite, i want a function to be called. However, i only want it to work if the user isnt holding down the left mouse button, so that he has to "drop" the sprite for the collision to trigger.
Here is the code:
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.name == "IconPlus" && !Input.GetMouseButtonDown (0)) {
//transform.GetComponent<Renderer> ().material.color = Color.green;
//Destroy(this.gameObject);
//mainScript.inputNumber += ballnumber;
Debug.Log ("Hit!");
}
}
If i remove the "&& !Input.GetMouseButtonDown(0)", the collision works fine. Please help.
Answer by tmalhassan · Oct 22, 2017 at 06:29 PM
Hello @chainylol. I guess what you're looking for is Input.GetMouseButtonUp(0)
. So what we do is check if the mouse is up and set the bool isBreakable to true. And once the object enters the trigger, it will register the hit. Of course, we will need the set the bool back to false so it would enable us to do it again. Except, there will be a drawback to this, because if the player clicks and releases anywhere else on the screen, it will set the bool to true. And once the object enters the trigger, it will register the hit. But luckily, you can work around this issue by creating another bool isHoldingObject to be set to true on OnMouseDrag()
function and add the bool to the if statement condition in Update()
. So the code would look like this:
private bool isBreakable = false;
private bool isHoldingObject = false;
float distance = 10;
int hitEnabler = 0;
void Update()
{
if (Input.GetMouseButtonUp(0) && isHoldingObject == true)
{
hitEnabler = 0;
isBreakable = true;
isHoldingObject = false;
}
}
void OnTriggerStay2D(Collider2D other)
{
if (hitEnabler == 0)
{
if (other.gameObject.name == "IconPlus" && isBreakable == true)
{
//transform.GetComponent<Renderer> ().material.color = Color.green;
//Destroy(this.gameObject);
//mainScript.inputNumber += ballnumber;
Debug.Log("Hit!");
}
}
hitEnabler = 1;
isBreakable = false;
}
void OnMouseDrag()
{
isBreakable = false;
isHoldingObject = true;
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = objPosition;
}
Take note that this script has to be attached to the object that you're dragging. Also make sure that the object has a Rigidbody 2D component.
Hope this gives you a general idea on how to work around your issue, and all the best :)
Hey man, thanks for the answer. I think you might be right about Input.Get$$anonymous$$ousebutton having to be used in the Update(), so i made a bool called "isBreakable"t hat turned false if the mouse button is held down inside the update() function.
I then added "&& isBreakable" to the if statement. It still doesnt work if i drag and drop the sprite like i want to, but if i place the sprite above the collider and let it fall down on it, the hit will register. Super weird.
No, it won't work that way either. But you're close to getting the idea. I just updated my answer, take a look at it :)
@tmalhassan Hmm, i copy/pasted your script, but it still doesnt work. Really dont know why...
Weird, it worked for me. Did you make sure that On$$anonymous$$ouseDown()
function is in a script attached to the object that you're dragging? I also forgot to mention that you need a collider on the object that you're dragging, and that collider must not be set to trigger.
one object (the one im dragging) is a 2d sprite and the other one is a UI image. Both have circle colliders 2D set to trigger. The collision works fine without the Input.Get$$anonymous$$ouseButton part. I added the On$$anonymous$$ouseDown() function in the same script, and attached it to the object im dragging.
After the first collision has happened, it doesnt even matter if i hold down L$$anonymous$$B or not, It triggers anyway.