I’m having a slight issue with my game, I’m trying to use Boxcollider2D.OverlapCollider to gather information on what colliders my player is hitting. However the issue comes into player where the boxcollider on my player is not touching the boxcollider of an npc resulting in the smallest gap between the two colliders.
One thing to note is that I’m using a manual collision detection by using a boxcast to determine whether or not the player can move through objects with colliders. I know the code works as if I manually enlarge the boxcollider of the player to overlap the npc’s collider I get Debug.log saying that Im hitting the npc.
I’m following a tutorial and followed through however I can’t continue as I have this issue. The tutorial doesn’t run into the same issue as me. I want to see how I can fix the issue of the two colliders not being able to overlap each other.
Many thanks.
// Using a box cast to test whether or not the player can move
void CollisionDetection(float x, float y)
{
hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, moveDelta.y), Mathf.Abs(moveDelta.y * moveSpeed * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));
if (hit.collider == null)
{
transform.Translate(0, moveDelta.y * moveSpeed * Time.deltaTime, 0);
}
hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.x * moveSpeed * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));
if (hit.collider == null)
{
transform.Translate(moveDelta.x * moveSpeed * Time.deltaTime, 0, 0);
}
}
public class Collidable : MonoBehaviour
{
public ContactFilter2D filter;
private BoxCollider2D boxCollider;
private Collider2D[] hitResults = new Collider2D[10];
// Start is called before the first frame update
protected virtual void Start()
{
boxCollider = GetComponent<BoxCollider2D>();
}
// Update is called once per frame
protected virtual void Update()
{
boxCollider.OverlapCollider(filter, hitResults);
for (int i = 0; i < hitResults.Length; i++)
{
if(hitResults[i] == null)
continue;
Debug.Log(hitResults[i].name);
hitResults[i] = null;
}
}
}
You should NEVER modify the Transform when using 2D physics to cause movement. Use a Rigidbody2D and move via its API only.
Don’t use Physics2D.BoxCast and read a BoxCollider2D details (or try to). Simply use Collider2D.Cast on the BoxCollider2D itself. It’s confusing because you do this for overlaps below in your class!!
Use the query overloads that allow you to pass in a List for results and reuse the list. That ways you don’t have to create a set of results big enough to fit, they’ll be sized automatically
When using a query and providing an array, the number of results found are returned by the query. The array length is NOT the number of results, that’s just how big an array you created!!
boxCollider.OverlapCollider(filter, hitResults);
for (int i = 0; i < hitResults.Length; i++)
{
if(hitResults[i] == null)
continue;
Debug.Log(hitResults[i].name);
hitResults[i] = null;
}
In terms of small gaps, these are added by the physics system to ensure stability. These gaps are tiny but look big if your scales are small.
They are controlled by Physics2D.defaultContactOffset and are only used on polygon collisions. Normally this is a compiler-time constant but we surfaced this so you can change it but it’s not something to change without consequence though. If you want stable polygon collision detection for stacking or other such dynamic collision detection then changing this can cause stabilty issues.
You are free to experiment with it however to see if this is the issue you are referring to.