Collision detection is driving me insane

I’m trying to program an enemy that follows the player and jumps upon two conditions:
A. the player is within a certain cone above the enemy
B. there is a tilemap block in front of the enemy, aka an obstacle to jump over

criteria A hasn’t given me any problems, but B is beginning to drive me insane.
I spent a long time trying to use Physics.CheckCapsule and Physics.CapsuleCast, since they return a boolean value of true or false, but “isBlocked” never changed from false when I used this setup:

public class enemyAI : MonoBehaviour
{

[SerializeField] bool isBlocked(float length, float radius)
{
return Physics.CheckCapsule(new Vector3(transform.position.x + length, transform.position.y, transform.position.z), new Vector3(transform.position.x - length, transform.position.y, transform.position.z), radius);
} //i tried the same thing with CapsuleCast, including the return at the beginning. yielded the same results.

void Update{

if (isBlocked(3, 0.5f) && grounded)
{
jump = true;
}

Finally I got results by using Physics.OverlapCapsule, but now jump is constantly being set to true, and the enemy bounces everywhere. I don’t know what i’m doing wrong. The current script is attached.

9729733–1391221–enemyAI.cs (4.21 KB)

You’re trying to do 3D physics queries (Physics.OverlapCapsule) against 2D colliders. That’s not going to work. You need to use the appropriate 2D physics queries.

For example Unity - Scripting API: Physics2D.OverlapCapsule

1 Like

Oh my gosh, that’s such an obvious fix, i dont know how i didnt think of that! thanks so much!