Search Unity

OverlapCircle not working as intended

Discussion in 'Getting Started' started by ShedoPix, Feb 27, 2023.

  1. ShedoPix

    ShedoPix

    Joined:
    Jan 26, 2023
    Posts:
    2
    I'll just go straight to the point, I'm using OverlapCircle to detect if my character is grounded by creating one at the bottom of my character that will detect if it's touching any object that is not in my "Player" collision layer. And thus allowing the player to jump when it is grounded.

    The script worked fine until I realised that, instead of checking if there's an object in the radius of the OverlapCircle, it's checking if an object touches my character collider. Thus allowing the player to jump while being in mid air but touching something else like, say, a wall.

    This happens with any part of the character, the sides, the top, etc. It's like the OverlapCircle took the shape of the character collider, I once even changed the ground to the "Player" collision layer and added a small square at the height of the the upper half of the player, and, as aspected, the player was only able to jump when it was touching that small square that wasn't even near the character's bottom.

    This is the jump code:

    upload_2023-2-26_23-41-24.png
    (OtherLayers is referencing every layer that isn't the "Player" one)

    And I used Gizmos to see how the OverlapCircle should look:

    upload_2023-2-26_23-46-40.png upload_2023-2-26_23-47-43.png

    I've looked everywhere for a solution to this but I could not find anything.
     
    Last edited: Feb 27, 2023
  2. AnaWilliam850

    AnaWilliam850

    Joined:
    Dec 23, 2022
    Posts:
    36
    It seems that the issue is caused by the fact that the Physics2D.OverlapCircle method is detecting collisions with any collider that is not in the "Player" layer, including colliders that are not below the player's feet.

    To fix this, you can modify your code to cast a raycast downwards from the player's feet and check if it hits a collider that is not in the "Player" layer. Here's an example:

    csharp
    public class PlayerController : MonoBehaviour
    {
    public float jumpForce = 10f;
    public float groundDistance = 0.2f;
    public LayerMask otherLayers;
    public Transform groundCheck;

    private Rigidbody2D rb;

    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
    bool isGrounded = Physics2D.Raycast(groundCheck.position, Vector2.down, groundDistance, ~otherLayers);
    if (isGrounded && Input.GetKeyDown(KeyCode.Space))
    {
    rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    }
    }

    void OnDrawGizmosSelected()
    {
    Gizmos.color = Color.yellow;
    Gizmos.DrawWireSphere(groundCheck.position, groundDistance);
    }
    }

    In this example, we are using a groundCheck transform to cast the raycast downwards. The isGrounded variable is set to true if the raycast hits a collider that is not in the "Player" layer within a distance of groundDistance. If isGrounded is true and the player presses the spacebar, the player will jump.

    Note that the OnDrawGizmosSelected method is used to draw a yellow wire sphere in the Unity Editor to visualize the ground check distance.

    I hope this helps! Let me know if you have any questions.
     
  3. ShedoPix

    ShedoPix

    Joined:
    Jan 26, 2023
    Posts:
    2
    So I adjusted my script to use a Raycast instead of an OverlapCircle, and it worked!
    Thanks a lot!

    But I still don't really understand why the OverlapCircle didn't work, I even tried making it really big and it still only worked when something was touching the character's collider. Strange.

    But anyways, I don't really care about that now, I'll try using Raycast more since I can have a lot more control over a line than a circle.