Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Collision detection is driving me insane

Discussion in 'Scripting' started by douglasdaszewski, Mar 27, 2024.

  1. douglasdaszewski

    douglasdaszewski

    Joined:
    Feb 4, 2024
    Posts:
    2
    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.
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,931
    spiney199 likes this.
  3. douglasdaszewski

    douglasdaszewski

    Joined:
    Feb 4, 2024
    Posts:
    2
    Oh my gosh, that's such an obvious fix, i dont know how i didnt think of that! thanks so much!