public override void OnInspectorGUI() { DrawDefaultInspector(); if (GUILayout.Button("Test Pathfinding")) { var enemy = GameObject.FindGameObjectWithTag("Enemy"); var player = GameObject.FindGameObjectWithTag("Player"); var start = enemy.transform.position; var destination = player.transform.position; var radius = enemy.GetComponent <CircleCollider2D>().radius; var pathfinder = new AndersonsAlgorithm( start: start, destination: destination, radius: radius ); Debug.Log("Found a route:"); Vector3[] vertices = pathfinder.Vertices(); foreach (Vector3 v in vertices) { Debug.Log(v); } enemy.GetComponent <Rigidbody2D>().velocity = (vertices[0] - start); } }
private void CalculatePath() { var pathfinder = new AndersonsAlgorithm( start: transform.position, destination: target.transform.position, radius: ColliderRadius() ); Path = pathfinder.Vertices(); }
private Vector3[] TryThroughElbow() { // Stop if out of steps if (RemainingSteps == 0) { return(null); } foreach (float angle in Angles()) { var direction = ToDestination(angle); Debug.DrawRay(Start, direction, Color.red, 5.0f); // Get distance to nearest obstacle in this direction var hit = ObstacleInDirection(direction); float maxDist = (hit.collider != null) ? hit.distance : 20.0f; float dist = 1.0f; while (dist < maxDist) { // Find elbow position dist from start in direction var elbow = Start + (dist * Vector3.Normalize(direction)); // In order to understand recursion... var pathfinder = new AndersonsAlgorithm(elbow, Destination, Radius, RemainingSteps - 1); Vector3[] vertices = pathfinder.Vertices(); if (vertices != null) { // It really shouldn't be this hard to prepend elbow to vertices! var list = vertices.ToList(); list.Insert(0, elbow); vertices = list.ToArray(); // Found a route through the elbow. // Bubble back up thorugh call stack. return(vertices); } // No route found thorugh this elbow. // Increment distance and try again. dist += 1.0f; } } // No route found through any elbow. // Give up. return(null); }