Question re: collision detection using Raycast2D (raycast gets "stuck?")

Hello! I’m making a simple 2D platformer and I wanted to use Unity’s Raycast2D feature to check collisions. I have a scene set up, and everything is working fine so far, only sometimes after jumping, the raycast gets “stuck” in the ground instead of “stopping” the player. Does anybody know why this might be? Am I wrong for even trying to detect collisions in this way?

(below is the code in my update function)

        /** RAYCASTING */

        //send out a ray detecting whether or not you are on ground
        Ray2D ray = new Ray2D(new Vector2(transform.position.x, transform.position.y), new Vector2(0, -1));
        Debug.DrawRay(ray.origin, ray.direction * 0.5f, new Color(255f, 0f, 0f));
        RaycastHit2D rayHit = Physics2D.Raycast(ray.origin, ray.direction, 0.5f);

        if (rayHit)
        {
            onGround = true;
            yVelocity = 0;
        }
        else
        {
            onGround = false;
            yVelocity += gravity;
        }
     

        /** CONTROLS */

        //if jump, then jump!
        if (Input.GetButtonDown("Jump") && onGround == true) {
            yVelocity += jumpVelocity;
            onGround = false;
        }
        //horizontal movement
        xVelocity = acceleration * Input.GetAxis("Horizontal");

        /** UPDATE POSITION */

        //update currentPosition to += xVelocity, yVelocity
        currentPosition.x += xVelocity * Time.deltaTime;
        currentPosition.y += yVelocity * Time.deltaTime;
        //update transform.position to == currentPosition
        transform.position = currentPosition;
        //then slow down xVelocity
        xVelocity *= 0.9f;

Well, what kinda of collisions do you want to detect? Movement and groundcheck, like it seems, are not what I’d use for that. The Unity tutorial actually has some pretty good insights into using a groundCheck for that. Raycasting is used more for ‘seeing’ things and projectiles and such.

Try and use a groundcheck object like in the tutorials and this kind of code;

// Decide on wether the player is grounded or not.
float groundRadius = 0.2f;
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);

groundCheck is a gameobject attached to the player, radius the sensitivity and whatIsGround the layers considered ground. Grounded then is boolean for yes or no grounded.

1 Like

Thank you for your response (I will switch to this kind of object-detection for walls and floors). However, after implementing this, I still end up with the same result of the player getting occasionally “stuck” in the ground. Here is a gif:

I wonder if this has anything to do w/ how I’ve implemented gravity? Here a part of my updated code:

        bool onGround = Physics2D.OverlapCircle(groundCheck.position, groundRadius);

        if (onGround)
        {
            yVelocity = 0;
        } else
        {
            yVelocity += gravity;
        }

I don’t think it has to do with gravity, and the getting stuck is quite common, the groundcheck should be at the bottom of your mob. And the collider should be around it. Mine looks like this, including two circle colliders, and never has the stuck issue on landing.

3187796--243313--Screen Shot 2017-08-18 at 10.00.44.png

1 Like