Hi,
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;
}
}
}