NavMesh.SamplePosition 采样位置 How to get a random point on NavMesh?

来源:互联网 发布:美国歌手prince 知乎 编辑:程序博客网 时间:2024/05/01 08:02

转自:  http://wiki.ceeger.com/script/unityengine/classes/navmesh/navmesh.sampleposition

转自:  http://answers.unity3d.com/questions/475066/how-to-get-a-random-point-on-navmesh.html


I generated a random point with in a unit sphere and multiplied it by the max distance I wanted my agents to walk:

 Vector3 randomDirection = Random.insideUnitSphere * walkRadius;

I then added my agents current position to this vector and used the NavMesh.SamplePosition function to find the close position on the NavMesh:

 randomDirection += transform.position; NavMeshHit hit; NavMesh.SamplePosition(randomDirection, out hit, walkRadius, 1); Vector3 finalPosition = hit.position;

I then used final postion as the destination for my agent.
Hope this helps!

0 0
原创粉丝点击