protected void Update() { ProcessInputAndUpdateUi(); // Enqueue a path queries using FindPath. Checking start or goal changed // is omitted for clarity. Use Pathfinder recalculate flags to indicate which // states trigger automatic recalculation of an agent's path. _agent.FindPath(_goal.position); // Manually trigger navmesh and pathfinder update to ensure the visuals line up Runner.ProcessModifications(); // A ray cast, make sure to dispose of the RayCastResult returned from // Navmesh.CastSegment or Navmesh.CastRay var rbs = FindObjectsOfType <RaycastBehaviour>(); foreach (var rb in rbs) { var from = rb.GetStart(); var to = rb.GetGoal(); if (!Plane.Contains(from) || !Plane.Contains(to)) { continue; } var pointSize = .1f * _cameraController.Zoom; using var result = _navmesh.CastSegment(from, to, true); _lineDrawer.DrawLine(from, to, result.CollisionDetected ? CastHitColor : CastColor); var hits = result.Hits; for (int i = 0; i < hits.Length; i++) { _lineDrawer.DrawPoint(hits[i].Position, i == 0 ? CastHitColor : CastColor, pointSize); } } // A disc cast, make sure to dispose of the DiscCastResult returned from // Navmesh.CastDisc var dbs = FindObjectsOfType <DiscCastBehaviour>(); foreach (var db in dbs) { if (!Plane.Contains(db.Centre)) { continue; } using var result = _navmesh.CastDisc(db.Centre, db.Radius, false); _lineDrawer.DrawCircle(db.Centre, new Vector2(0, db.Radius), 2 * Mathf.PI, result.CollisionDetected ? CastHitColor : CastColor, res: 200); } }
protected void Update() { ProcessInputAndUpdateUi(); _agent.FindPath(_goal.position); var rbs = FindObjectsOfType <RaycastBehaviour>(); foreach (var rb in rbs) { var from = rb.GetStart(); var to = rb.GetGoal(); if (!Plane.Contains(from) || !Plane.Contains(to)) { continue; } var pointSize = .1f * _cameraController.Zoom; using (var result = _navmesh.CastSegment(from, to, true)) { _lineDrawer.DrawLine(from, to, result.CollisionDetected ? CastHitColor : CastColor); var hits = result.Hits; for (int i = 0; i < hits.Length; i++) { _lineDrawer.DrawPoint(hits[i].Position, i == 0 ? CastHitColor : CastColor, pointSize); } } } var dbs = FindObjectsOfType <DiscCastBehaviour>(); foreach (var db in dbs) { if (!Plane.Contains(db.Centre)) { continue; } using (var result = _navmesh.CastDisc(db.Centre, db.Radius, false)) _lineDrawer.DrawCircle(db.Centre, new Vector2(0, db.Radius), 2 * Mathf.PI, result.CollisionDetected ? CastHitColor : CastColor, res: 200); } }
void OnGUI() { if (GUILayout.Button("Add")) { // _id = Navmesh.InsertObstacle(new[] {new Vector2(1, 1), new Vector2(1, 5)}, Vector2.zero, Vector2.one, 0); _id = Plane.InsertObstacle(new[] { new Vector2(3, 0), new Vector2(3, -1) }); } if (GUILayout.Button("Remove")) { Plane.RemoveObstacle(_id); } if (GUILayout.Button("Path")) { Agent.transform.position = new Vector2(-5, 1).ToXxY(); Agent.FindPath(new Vector2(5, 1)); } if (GUILayout.Button("Spawn")) { Instantiate(ObstaclePrefab); } }