Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

how it detect is there is a floor below character?

Discussion in 'Physics' started by steveh2112, Mar 11, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    235
    i have the following code to detect a floor of some kind below the character
    Code (CSharp):
    1.  
    2.        RaycastHit hit;
    3.         //LayerMask mask = LayerMask.GetMask("TransparentFX");
    4.         if (Physics.Raycast(transform.position, Vector3.down, out hit, 2)) //, ~mask))
    5.         {
    6.             Debug.Log("Ai Ai_Movement something below ai " + name + " hit " + hit.collider.gameObject);
    7.             return true;
    8.         }
    9.  
    10.         Debug.Log("Ai Ai_Movement nothing below ai " + name);
    11.  
    it works sometimes but then for no obvious reason when moving over the terrain, it fails to detect a ground hit

    any idea why and how to make it work reliably?

    thx
     
  2. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    235
    i think the problem was the start point was too close to the ground, so by starting a bit higher it seems ok now

    Code (CSharp):
    1. if (Physics.Raycast(transform.position + new Vector3(0,.1f, 0), Vector3.down, out hit, 2))
     
unityunity