Search Unity

Question [Solved] SphereCast can't find stairs

Discussion in 'Physics' started by Max-0, Nov 30, 2023.

  1. Max-0

    Max-0

    Joined:
    Jun 10, 2021
    Posts:
    31
    Hello everyone! I am having an issue with Physics.SphereCast. I am trying to cast a sphere at the bottom of the player to detect stairs, get the point where it hits, and move the player up to there.
    It works great on the first stair, the player moves up.
    upload_2023-11-29_19-9-32.png

    Here is the code responsible for this:
    upload_2023-11-29_19-22-50.png
    And here is code for the blue sphere gizmo, for context.
    upload_2023-11-29_19-11-16.png
    On the second stair, and other stairs, it does not detect any hits from the SphereCast. upload_2023-11-29_19-13-2.png
    I initially thought it was because it was hitting the stairs, but it actually just didn't hit anything somehow.

    Any help is much appreciated!
     

    Attached Files:

    Last edited: Nov 30, 2023
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,421
    SphereCast will not find a collider that the sphere is already overlapping at the starting position. It will only find colliders which will be struck with a nonzero movement in that direction. You can start your SphereCast higher than the bottom of your character to see if there's something you may already be sinking into. Or see also OverlapSphere.
     
    zulo3d likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    990
    If the sphere cast is already penetrating an object from the beginning of the cast then it will ignore the object. Is your stairs a single mesh or are those steps made from individual cubes?. I'm guessing it's a single mesh and the cast fails because it was already penetrating the stairs mesh on the first step.
     
  4. Max-0

    Max-0

    Joined:
    Jun 10, 2021
    Posts:
    31
    Thank you both very much for the answer, I can't believe I forgot about that!