private static void MoveNext(WanderClient client, bool append) { if (!client.unit.isAlive) { StopWander(client.unit); return; } Vector3 pos = Vector3.zero; int attempts = 0; while (attempts < bailAfterFailedAttempts) { pos = client.unit.position + (Random.insideUnitSphere.normalized.OnlyXZ() * Random.Range(client.minimumDistance, client.radius)); var grid = GridManager.instance.GetGrid(pos); if (grid != null) { var cell = grid.GetCell(pos, true); if (cell.IsWalkableWithClearance(client.unit)) { client.unit.MoveTo(cell.position, append); return; } } attempts++; } //If we didn't find a wander point, stop wandering. StopWander(client.unit); }
private static void Start(WanderClient client) { MoveNext(client.unit.gameObject, false); if (client.lingerForSeconds == 0.0f) { MoveNext(client.unit.gameObject, true); } }
/// <summary> /// Makes the specified unit wander around at random. /// </summary> /// <param name="unit">The unit.</param> /// <param name="radius">The radius within which to wander (seen from the unit center).</param> /// <param name="minimumDistance">The minimum distance to wander, each time a new destination is picked.</param> /// <param name="lingerForSeconds">How many seconds to linger at each destination before moving on.</param> public static void Wander(this IUnitFacade unit, float radius, float minimumDistance, float lingerForSeconds) { if (_handler == null) { _handler = new Handler(); } var client = new WanderClient { radius = radius, minimumDistance = minimumDistance, lingerForSeconds = lingerForSeconds, unit = unit }; _clients.Add(unit.gameObject, client); Start(client); }