Question by
OTTwarrior · Apr 12 at 09:24 AM ·
ainavmeshnavmeshagentrandomizationai problems
how to search for a random point until the condition is true?
while hasPoint is false, i want to search for a random point until the distance between the point position and the player (target) position is higher than 30. If i find one, hasPoint needs to become true and THEN i need to execute other lines of code and return the finalPosition value. How can i do this?
{ public Vector3 RandomNavMeshLocation()
{
bool hasPoint = false;
Vector3 randomPosition = Random.insideUnitSphere * lookRadius;
while (!hasPoint)
{
randomPosition = Random.insideUnitSphere * lookRadius;
if (Vector3.Distance(randomPosition, target.position) > 30f)
{
hasPoint = true;
Debug.Log("has point");
}
}
if (hasPoint == true)
{
randomPosition += transform.position;
Debug.Log("added");
}
NavMeshHit hit;
NavMesh.SamplePosition(randomPosition, out hit, lookRadius, 1);
Vector3 finalPosition = hit.position;
return finalPosition;
}
Comment