Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Raycast returning weird normals at corner

Discussion in 'Physics' started by SgtSargeMcFly, Feb 24, 2023.

  1. SgtSargeMcFly

    SgtSargeMcFly

    Joined:
    May 4, 2020
    Posts:
    20
    I've been periodically working on a sidescroller and want the player to be able to move perpendicular to the ground they're standing on. I do this using a raycast (or circlecast to be specific) but I have a problem with simple platforms.

    upload_2023-2-23_20-31-16.png

    Here, the red line is the down vector and the yellow line is the forward vector. This is how I want them to look no matter where the player is on the platform, but when you go to the corner:

    upload_2023-2-23_20-32-55.png

    It returns the normal as if the box collider had rounded edges, which I don't want it to do. However I can't find a way. The edge radius on the box colliders is already 0, and I seem to have the same problem with all the different colliders.

    Is there any way I can get rid of the "invisible" rounded edges and just make this box collider act like you'd expect them to in a game like this?
     

    Attached Files:

  2. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    841
    Spherecasts have the issue that they return the normal of the collision against their spherical shape, so effectively the normal you are getting is due to the fact that the spherecast is well, checking for the collision of a sphere against that corner, which or course can have a collision normal is a large range of values depending on where exactly it hit that corner. You might have to combine this spherecast with one-or-more raycasts to get exactly precise results for situations like this.
     
  3. SgtSargeMcFly

    SgtSargeMcFly

    Joined:
    May 4, 2020
    Posts:
    20
    I could swear I tried this before and it didn't work but sure enough yeah it's working for me now, I decided that once the spherecast hit something I would then cast a normal ray towards the point it hit and have that return the normal to me instead. Thanks for chipping in.
     
    Sluggy likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    This is nothing to do with CircleCasts. All polygons can be hit on an edge or vertex when doing a shape cast. The collision normal of the polygon surface (a Box in this case) when contacting a vertex will interpolate between the two adjacent edge normals.
     
    davidheeren and Sluggy like this.
  5. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    841
    @MelvMay That's very good to know. I guess I've just noticed it more with sphere colliders due to the large surface contact area and the fact that they are much more like to pass over that corner vertex at a variety of angles.
     
    MelvMay likes this.