Search Unity

Question how to check if player is standing on a walkable part of a navmesh?

Discussion in 'Scripting' started by SpyderManToo, Jul 6, 2021.

  1. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    i need help, im trying to make the ground check in my fps with navmesh bc physics.checksphere doesnt work and neither does raycast, help

    how do i check if the player is standing on a part of the navmesh that you can walk on? here is my code rn
    Code (CSharp):
    1. NavMeshHit hit;
    2.             if (NavMesh.Raycast(groundCheck.position, Vector3.up * -1, out hit, NavMesh.GetAreaFromName("Walkable")))
    3.             {
    4.                 canJump = true;
    5.             }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    It's what I use to check if my agent is positioned on a navmesh. The agent won't bind to the navmesh if it isn't close enough to it.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Interesting, does it unbind if the agent is forced off of the NavMesh? I haven't experimented enough with them to know the details.
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Yes.
     
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Also, NavMesh.SamplePosition won't as such just tell if your agent is over a navmesh at its current position, it will find the closest point on the navmesh within a given radius of the position supplied or return false if nothing in range.
     
    Last edited: Jul 6, 2021
  9. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    i am not trying to use this for an agent, i am trying to use this for my player as a ground check
     
  10. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    i wrote this,
    Code (CSharp):
    1. NavMeshHit hit;
    2.             if (NavMesh.SamplePosition(groundCheck.position, out hit, 0.1f, NavMesh.GetAreaFromName("Walkable")))
    3.             {
    4.                 canJump = true;
    5.             }
    but its returning as false...
     
  11. yatyricky

    yatyricky

    Joined:
    Dec 10, 2014
    Posts:
    3
    Change `NavMesh.GetAreaFromName("Walkable")` to `1 << NavMesh.GetAreaFromName("Walkable")`
     
  12. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    dont necro
     
  13. dmaheshmahid

    dmaheshmahid

    Joined:
    Aug 3, 2021
    Posts:
    1
    Do you got any solution?
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    You're replying to a post saying not to necro threads by necroing the thread.
     
  15. WayneDavidDaley

    WayneDavidDaley

    Joined:
    Jul 13, 2014
    Posts:
    2
    I'm attempting to do something similar were you ever able to determine if a position was within a specific navmesh mask?
     
    RatherGood likes this.
  16. SI_007

    SI_007

    Joined:
    Aug 10, 2015
    Posts:
    84
    Thanks Yatyricky, much appreciated your solution.

    Code example:

    public Vector3 walkPoint;
    public bool walkPointSet;
    public float walkPointRange;

    public void SearchWalkPoint()
    {
    float randomZ = Random.Range(-walkPointRange, walkPointRange);
    float randomX = Random.Range(-walkPointRange, walkPointRange);
    walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);

    NavMeshHit hit;
    if (NavMesh.SamplePosition(walkPoint, out hit, 0.1f, 1 << NavMesh.GetAreaFromName("Walkable")))
    { walkPointSet = true;}
    }
     
    tutrann20 likes this.