Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    314
    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
     
    du57mi73 likes this.
  2. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    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))
     
    du57mi73 likes this.