- Home /
Box collider 2d detecting collisions while not actually colliding
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
Answer by narasu · Apr 24, 2019 at 08:49 AM
I just found the solution. Turns out it's probably a good idea to set a Rigidbody to kinematic when you're about to influence it directly. I also changed the collider on my tilemap to use Polygons instead of Outline. Everything works perfectly now.
Your answer
Follow this Question
Related Questions
Player ignores tilemap collider on fall 1 Answer
Tilemap Collider question. 2 Answers
Distribute terrain in zones 3 Answers
Problem with disabled collision in 2D objects 0 Answers
Tilemap edge-outline collider 1 Answer