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)