public IEnumerator wander() { Vector3 newPos = RandomNavmeshLocation(wanderRadius); // gets random point on navmesh while (true) { NavMeshPath path = new NavMeshPath(); navMeshAgent.CalculatePath(newPos, path); if (path.status != NavMeshPathStatus.PathComplete) // checks to make sure that a path can be calcultaed with given random point { Debug.Log("Invalid path"); newPos = NavMeshUtils.RandomNavSphere(transform.position, wanderRadius, -1); } else { break; } } AICurrentTargetLocation = newPos; navMeshAgent.SetDestination(newPos); yield return new WaitUntil(() => Vector3.Distance(gameObject.transform.position, newPos) <= 0.25f); wanderCoroutine = null; }
public void RandomTargetPos() { Vector3 targetPos = NavMeshUtils.RandomPointOnMap(NavMesh.AllAreas, _mapPivotReference.localScale.x, _mapPivotReference.localScale.z, _mapPivotReference.localScale.magnitude, _mapPivotReference.transform.position); _agent.SetDestination(targetPos); }
void RefreshUI() { float pathDistance = NavMeshUtils.GetAgentCurrentDestinationPathDistance(snapTarget.GetNavMeshAgent()); float estimatedTime = pathDistance / snapTarget.GetMaxSpeed(); if (pathDistance < 0) { pathDistance = 0; estimatedTime = 0; } remainingDistanceText.text = "Distance left: " + pathDistance.ToString("f1"); remainingTimeText.text = "Estimated time left: " + estimatedTime.ToString("f1") + "s"; }
private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawLine(NavMeshUtils.Place(transform.position), NavMeshUtils.Place(transform.position) + transform.forward * Distance); Gizmos.color = Color.blue; Vector3[] points = NavMeshUtils.MovePosition(transform.position, transform.forward * Distance, Quality); if (points.Length >= 2) { for (int i = 1; i < points.Length; i++) { Gizmos.DrawSphere(points[i], 0.05f); Gizmos.DrawLine(points[i - 1], points[i]); } } }
//Refresh path if target is an object (because some object can move) void DrawPath(Vector3[] corners) { if (currPath != null && currPath.corners.Length > 1) { lineRenderer.positionCount = corners.Length; lineRenderer.SetPositions(corners); int currCornerIndex = NavMeshUtils.GetCurrentPathCornerIndex(agent, corners); for (int i = 0; i < currCornerIndex; i++) { lineRenderer.SetPosition(i, transform.position); } } else { lineRenderer.positionCount = 0; } }
public Vector3 GetRandomPointOnMap() { return(NavMeshUtils.RandomPointOnMap(NavMesh.AllAreas, _mapPivotReference.localScale.x, _mapPivotReference.localScale.z, _mapPivotReference.localScale.magnitude, _mapPivotReference.transform.position)); }
private float GetDistanceToTarget() { return(NavMeshUtils.CalculatePathLength(context.NavigationAgent.transform.position, GetTargetPosition())); }