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 OnCollisionEnter grounded check

Discussion in 'Physics' started by Mangonels, Jun 24, 2023.

  1. Mangonels

    Mangonels

    Joined:
    May 8, 2018
    Posts:
    20
    I'm attempting grounded validation with OnCollisionEnter/Exit.

    My player game object has a capsule collider, and a lower sphere collider.

    upload_2023-6-24_19-44-22.png

    In a script with OnCollisionEnter()/OnCollisionExit() I'd like to verify collisions with my sphere collider ONLY, ignoring collisions with the capsule collider, for ground detection purposes.

    (this is because I have the capsule collider as a general frictionless volume so that it doesn't stick to walls, and the sphere as a lower and smaller ground friction volume, that I'd also like to use as a grounded checker)

    Previously I was doing this with OnTriggerEnter and a 3rd collider. Of course, this worked without discerning where about the game object (including children) the trigger enter happened, because there was only one trigger collider in the whole G.O.

    Also, having a trigger based grounded detection means the player controlled character could be considered grounded a few units above the ground, I need something more precise like OnCollisionEnter.

    Another alternative would be raycasting, but that could have the same slight precision issue (+ I believe it's a bit more resource intensive?) I think I'd rather try out OnCollisionEnter/Exit for this.

    So in essence, can I do grounded checks with OnCollisionEnter, maybe validating that the collider that received the collision (my sphere collider, the one that is in the same G.O. as the OnCollisionEnter script) is the sphere one?
     
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    I think for ground detection purpose a raycast will work better. Position your raycast origin accurately so that it doesn't hit your own colliders from inside, and adjust the magnitude is just right so that it will hit the ground within a threshold distance.

    Don't worry about resources, it is quite normal to use even several raycasts every frame for your character's ground checking or collision awareness. A common technique would be to direct the raycast to an angle that is both forward and downward relative to your character's transform. Or you could use 3 raycasts where one points forward+down, and two that point to the sides likewise, for better readings. Then you can extract distance and normal information from the RaycastHit results, and determine eg if you're going level, uphill or downhill.
     
    Last edited: Jun 29, 2023
  3. driftnumata

    driftnumata

    Joined:
    May 31, 2023
    Posts:
    29
    I think if you use a ray cast, it would be good to use the Sphere cast or Capsule cast.