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

Question Agent only goes to edges and corners (NavMesh.SamplePosition())

Discussion in 'Navigation' started by emirayranci18, Feb 10, 2023.

  1. emirayranci18

    emirayranci18

    Joined:
    Sep 18, 2020
    Posts:
    3
    I want to make a little horror game. Here, I want the clown, who is our enemy, to wander around the house, so I researched going to random points on the internet and found this code.

    Code (CSharp):
    1.  
    2.                 Vector3 randomDirection = Random.insideUnitSphere * wanderRadius;
    3.                 randomDirection += transform.position;
    4.                 NavMeshHit hit;
    5.                 NavMesh.SamplePosition(randomDirection, out hit, wanderRadius, -1);
    6.  
    7.                 Vector3 finalPosition = hit.position;
    8.                 myAgent.SetDestination(finalPosition);
    9.  
    10.                 cubeX.transform.position = finalPosition;
    When I use this code, it only goes to the corners and edges in the agent area. In this video, for you to understand this better, this point is where the cube is, the position specified at the end of sample position.


    What I want is to determine random points within the blue area and walk through those points. How can I do this or where am I going wrong?
     
    muhammad_ali_safdar likes this.