Hi,
I was implementing a function that gets a random point on a circumference around the player to spawn coins when the player takes damage from an enemy. This function checks whether the random position is reachable for the player. It’s important to note that there may be multiple coins, so the function will be called multiple times within a single frame.
Vector3 randomPos = GetRandomPoint();
Vector3 closestNavMeshPos = AstarPath.active.GetNearest(randomPos, NNConstraint.Walkable).position;
randomPos.Set(closestNavMeshPos.x, randomPos.y, closestNavMeshPos.z);
Here, GetRandomPoint
retrieves a random point on the circumference.
I’ve been reading some documentation and forum posts, and I’ve concluded that using only one call to GetNearest
should be the most efficient option.
I believe NNInfo.position
returns the closest point on the node rather than the closest position to the original point. Therefore, if there are few nodes, the positions may overlap and not be evenly distributed, if I’m correct.
Is there a better or more efficient way to do this? Perhaps retrieving some graph points to make the coin positions more accurate relative to the original random position or checking if the random point is reachable?
I’m using a NavMesh graph.
Thanks in advance!
- A* version: 5.1.6
- Unity version: 2023.2.13f1