Search Unity

Detecting the surface / slope and edges

Discussion in 'Scripting' started by CortiWins, Feb 11, 2020.

  1. CortiWins

    CortiWins

    Joined:
    Sep 24, 2018
    Posts:
    150
    Hello folks, i'll try to describe this as good as i can.

    I got an FPS controller and i don't want to be able to hop up walls like a mountain goat, so i build a function that lets the player slide down slopes that are too steep. I detect the angle by Physics.SphereCast to the feet of the controller which gives me a RaycastHit with a normal that i can use to calculate the angle with which i am standing on the surface, the slideDown-Direction and via Vector3.ProjectOnPlane the direction that movement could go.

    This works fine on normal slopes like in Example A) and B) within the image.
    However, it also detects collisions with edges like when i jump up somewhere like in C) and on other edges likes in D).

    Is there a way to find out how the surface my Raycast has hit is oriented?
     

    Attached Files:

  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    That's ... normal

    One of the biggest headache in 3d controller, trust me.

    But basically you are doing a sphere cast, ie the contact is a sphere, the normal returned will be point of impact TOWARD the center of the sphere stopped by that impact, ie a diagonal.

    What you want is to have multiple raycast beside the central one, to check for surfaces, then use the highest one.
     
    CortiWins likes this.
  3. CortiWins

    CortiWins

    Joined:
    Sep 24, 2018
    Posts:
    150
    @neoshaman
    Oh, thats interesting!

    Could you describe a little more where you would start and aim at those multiple raycasts?
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,493
    at the periphery of the collider, in a circle, try 8 rays (plus the middle one)