I’ve got an object with Box Collider 2D (trigger) that is always at the mouse position, and is detecting collisions even though it’s not actually colliding. I use the following script:
public class PlacementDisable : MonoBehaviour
{
public bool isColliding;
private void OnTriggerEnter2D(Collider2D col)
{
isColliding = true;
print(col.gameObject);
}
private void OnTriggerExit2D(Collider2D col)
{
isColliding = false;
}
}
Sometimes this prints the name of another object (e.g. Tilemap) even when the two are not actually colliding. Any ideas?
EDIT: I should add it’s also pretty janky when it IS colliding. Basically I have a bit of code that stops it from instantiating another copy on mouse click when it’s colliding, but that also fails a lot. I’ve got it set up like so:
private void PlaceObject(GameObject cObj)
{
PlacementDisable pd = cObj.GetComponent<PlacementDisable>();
if (Input.GetMouseButtonDown(0) && pd.isColliding == false)
{
GameObject o = Instantiate(cObj);
}
}
It tends to both place objects where it shouldn’t, and fail to place something at an empty spot