Ed Arévalo Ed Arévalo - 5 months ago 98
Android Question

Unity3D using OnTriggerStay

I'm using the event OnTriggerStay2D to destroy an object, the reason i'm using this instead of OnTriggerEnter2D is because i'm dragging the object using the touchscreen, and i want to destroy it after it is released, the problem i have is that OntriggerStay2D is not always called, so sometimes the object is not destroyed after it is released and it has to be moved again to work, i've read the docummentation from Unity


OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger.


public void OnTriggerStay2D(Collider2D other)
{
if (gameObject.tag == other.tag) {

Destroy (other.gameObject);

}
}


I would like to know if there's any way to call OntriggerStay2D everytime i release the object.
Thanks.

Edit
Dragging code


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Drag : MonoBehaviour {

private bool draggingItem = false;
private GameObject draggedObject;
private Vector2 touchOffset;

void Update ()
{
if (HasInput)
{
DragOrPickUp();
}
else
{
if (draggingItem)
DropItem();
}
}

Vector2 CurrentTouchPosition
{
get
{
Vector2 inputPos;
inputPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
return inputPos;
}
}

private void DragOrPickUp()
{

var inputPosition = CurrentTouchPosition;

if (draggingItem)
{

draggedObject.transform.position = inputPosition + touchOffset;

}
else
{

RaycastHit2D[] touches = Physics2D.RaycastAll(inputPosition, inputPosition, 0.5f);
if (touches.Length > 0)
{
var hit = touches[0];
if (hit.transform != null && hit.rigidbody != null)
{
draggingItem = true;
draggedObject = hit.transform.gameObject;
touchOffset = (Vector2)hit.transform.position - inputPosition;
}
}
}
}

private bool HasInput
{
get
{

return Input.GetMouseButton(0);
}
}

public void DropItem()
{
draggingItem = false;

}

}

Answer Source

Avoid using OnTriggerStay2D for this. You can use a boolean variable that you set to true and false in the OnTriggerEnter2D and OnTriggerExit2D function.

bool isTouching = false;

void OnTriggerEnter2D(Collider2D collision)
{
    Debug.Log("Entered");
    if (collision.gameObject.CompareTag("YourOtherObject"))
    {
        isTouching = true;
    }
}

void OnTriggerExit2D(Collider2D collision)
{
    Debug.Log("Exited");
    if (collision.gameObject.CompareTag("YourOtherObject"))
    {
        isTouching = false;
    }
}

You can now check the isTouching variable when the object is released.

if(isTouching){
....
}

Note that I suggest you abandon your current code that uses Raycast and Input.GetMouseButton(0); since you are using this on mobile devices too. You should be using Unity's new EventSystem for this since it is made to be mobile friendly too.

Since you are using 2D collider, see #7 from this answer.

Here is a complete example of how to drag a Sprite with the new EventSystem. Combine that with the answer above and you get a much more better solution.