Search Unity

Raycast 2D controller jumps

Discussion in 'Scripting' started by liquidsht, Jun 19, 2020.

  1. liquidsht

    liquidsht

    Joined:
    Jun 4, 2020
    Posts:
    49
    I have just gone through sebastian Lague's 2D raycast controller tutorial, everything is working much better in my metroidvania than with rigidbody.
    One issue I am having is when the player jumps and one of the top corners of the collider comes in contact with the bottom corners of platforms. With rigidbody, I was using Capsule collide 2D and no friction physics material, so the player's top edge just glides around the bottom corners of platforms when jumping. With the box collider raycast, it feels very jarring as you can feel it's feels like 2 square blocks with corners colliding, because that's what it is.

    Is there a way of getting around this with raycast but without rewriting the controller scripts?
    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Not sure what effect you're looking for but if you hard-stop the player when they hit their head, you can also raycast a little bit further and based on the distance begin to slow the player's upward velocity down a bit as they near the head-bump. Is that the jarring you refer to?
     
  3. liquidsht

    liquidsht

    Joined:
    Jun 4, 2020
    Posts:
    49
    My situation looks like this:
    ---[platform]
    [P]
    Both player and platform have box shaped colliders. So when player tries to jump onto platform but the are slightly overlapped on the X axis. The top right corner of player hits bottom left corner of platform, then the raycast detects hitting the platform then player begins to fall.
    What I want sort of looks like this:
    --[platform]
    0
    So the player has rounded top corners, so when the player jumps and the player's top right (now rounded) corner hits the bottom left corner of that platform, the player will still slip on to the platform. It's basically the opposite of player with capsule collider standing on edge of platform and begin to slip down.

    Sorry for the ugly diagrams, hope it helps explain my question. Thanks.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Oh right, gotcha... rounding the corners so things don't feel so "snaggy." Nintendo is the king of this stuff... they really get their "objects bumping" collisions spit-shined perfect every time.

    One approach is to do multiple raycasts:

    - raycast the center: if you hit, BAM, you banged your head, stop
    - raycast from center plus and minus lateral offset: if you hit here, then deflect your guy sideways while only reducing his upward velocity a little bit.

    Tinkering with the deflection forces / velocity changes you can usually get it feeling pretty good this way.

    corners.png
     
  5. liquidsht

    liquidsht

    Joined:
    Jun 4, 2020
    Posts:
    49
    The issue with a capsule collider is it ll slide down when standing on edge of platform. So I will need a circle/capsule collider with raycast for upper body and box collider for lower body? So I will need multiple raycast colliders? Do you know of any raycast tutorials with capsule colliders.

    I have just done some research and looks like circlecast might be able to work? but I am new to unity and programming so any help is appreciated. thanks.
     
    Last edited: Jun 24, 2020