- Home /
Question by
awplays49 · May 28, 2015 at 09:57 PM ·
pathfinding
Public Function Acting Differently when Called by Other Script
This function
public void GeneratePath (Vector3 startPos, Vector3 endPos) {
Node startNode = grid.WorldToNodePoint (startPos);
Node endNode = grid.WorldToNodePoint (endPos);
List <Node> openSet = new List <Node> ();
HashSet <Node> closedSet = new HashSet <Node> ();
openSet.Add (startNode);
while (openSet.Count > 0)
{
Node currentNode = openSet [0];
for (int i = 1; i < openSet.Count; i ++)
{
if (openSet [i].fCost < currentNode.fCost || openSet [i].fCost == currentNode.fCost && openSet [i].hCost < currentNode.hCost)
{
currentNode = openSet [i];
}
}
openSet.Remove (currentNode);
closedSet.Add (currentNode);
if (currentNode == endNode)
{
RetracePath (startNode, endNode);
return;
}
foreach (Node neighbor in grid.GetNeighbors (currentNode))
{
if (!neighbor.walkable || closedSet.Contains (neighbor))
{
continue;
}
int currentGCost = currentNode.gCost + GetDistance (currentNode, neighbor);
if (currentGCost < neighbor.gCost || !openSet.Contains (neighbor))
{
neighbor.gCost = currentGCost;
neighbor.hCost = GetDistance (neighbor, endNode);
neighbor.parent = currentNode;
openSet.Add (neighbor);
}
}
}
}
runs through the code (tested with Debug.Log)
when called by another script, but doesnt do anything. Help?
Call code:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
private GameObject grid;
private GameObject player;
private bool straightPath;
public float speed;
void Start () {
grid = GameObject.Find ("Plane");
player = GameObject.Find ("Player");
}
void Update () {
RaycastHit hit;
if (Physics.Raycast (transform.position, player.transform.position - transform.position, out hit, Mathf.Infinity))
{
if (hit.collider.tag == "Player")
{
straightPath = true;
}
else
{
straightPath = false;
}
}
if (straightPath == false || Vector3.Distance (transform.position, player.transform.position) > 4)
{
grid.GetComponent <PathGenerator> ().GeneratePath (transform.position, player.transform.position);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Pathfinding tools in Unity? 9 Answers
How to Implementing Morten Nobel-Jorgensen's A* Pathfinding 3 Answers
NavMesh Baking Problems 0 Answers
Best approach to generating a grid for basic A* pathfinding 1 Answer
Evade a Collider on Vector3.forward 3 Answers