Program says 2 BoxCollider2Ds are colliding, but they are not
I am new to Unity, and I have run into a problem that I cannot find the fix to.
The code below logs the colliders that are colliding with the BoxCollider2D of the character on the top right, but PF Player (the one on the left) is being logged (as seen in the screenshot), despite its box collider being far off from where the collider of the top right character is. (This only happens after I enter the BoxCollider2D of the right character, and before I enter it does not show that they are colliding.)
I have an exact copy of the character (the one that is being highlighted in the screenshot), but this one does not exhibit the same behaviors.
(As you can see in the screenshot, the console logs PF Player, even though the PF Player collider is not colliding with the collider of the character on the top right)
Code that I use to detect that they are colliding:
public ContactFilter2D filter;
private BoxCollider2D boxCollider;
private Collider2D[] hits = new Collider2D[10];
protected virtual void Start()
{
boxCollider = GetComponent<BoxCollider2D>();
}
protected virtual void Update()
{
boxCollider.OverlapCollider(filter, hits);
OnCollide(hits);
}
protected virtual void OnCollide(Collider2D[] coll)
{
foreach (Collider2D collider in coll)
{
if (collider == null)
{
continue;
} else
{
Debug.Log(collider.name);
}
}
}
Any ideas on how I can fix this? If there is any more information that is needed to know to fix, please let me know.
Thanks in advance :)
Your answer
Follow this Question
Related Questions
2D colliders doesnt work with Gravity Scale = 0 0 Answers
Object Colliding while passing along another object . 0 Answers
OnCollisionEnter2D not calling, but objects are colliding. 1 Answer
No overload for method. (Question) 0 Answers
How to check if any/multiple 2D sprites neighbour/collide with eachother 0 Answers