#Hey I've an issue with OverlapBox2D
1 messages · Page 1 of 1 (latest)
So I've this code that does an overlapbox behind the player in order to detect how far it can teleport backwards, each time it a crosses a collider it should shrink the overlap in order prevent the character from clipping through terrain. However it never seems to find a collider, which I'm not really sure why. I'm using Composite Colider 2D with Polygon colliders.
private Collision collision;
private float hailMaryDist = -2.5f;
private float incrementor;
private Vector2 center => new(transform.position.x + hailMaryDist + (incrementor / 2), transform.position.y);
private Vector2 size => new(hailMaryDist * 2 + incrementor, 0.3f);
private void OnEnable()
{
print(Collision.onCollision);
Collision.onCollision += () => StartCoroutine(Hail());
collision = GetComponent<Collision>();
print(Collision.onCollision);
}
private IEnumerator Hail()
{
print("Entered Hail State");
if (!collision.hailMary) yield break;
int overflow = 0;
print(Physics2D.OverlapBox(center, size, 0));
while (Physics2D.OverlapBox(center, size, 0))
{
print(Physics2D.OverlapBox(center, size, 0));
incrementor += 0.5f;
overflow++;
print("Gone into while");
yield return new WaitForSeconds(0.5f);
}
transform.position = center;
StartCoroutine(SlowdownEffect());
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireCube(center, size);
}
Hey I've an issue with OverlapBox2D
This is how it looks ingame
It just clips through the wall
Show the colliders that it's supposed to detect?
Kinda hard to see
Though I might've a better idea, instead of changing the transfrom I could just apply a rb force to it so it smashes into a wall instead
So I'll try that first
It's unclear to me what you're trying to accomplish here
Heh sorry, I'll tinker with it a bit more myself for now since I thought of this
If you're trying to not teleport through walls you should just do a box cast when you teleport
That will tell you exactly how far back you can go
Yeah but I could also just give it a rigidbody velocity backwards so that it won't teleport in the first place so that it naturally stops the player, right?