- Home /
What is wrong with my pathfinding script?
Does anyone know what is wrong with my pathfinding script? I have spent hours on it but made no progress. The script runs but the enemy won't move unless I get in sight of it. Any help would be greatly appreciated.
Before looking at the script here is a quick explanation of how it works. Empty gameobjects are placed in key positions around the map such as doorways and in the middle of rooms. I call these connectors. The script then goes through all of the connectors that can see the last object in the path (Such as the enemy or another connector), and chooses the one that is closest to the player. An important thing about the connectors is they each have a script that keeps track of how close they are to the player and whether or not the enemy and player are in sight. Whether or not the player is in sight is represented by the boolean, InSight.
Thanks for taking the time to looking over my script.
#pragma strict
var Connectors : GameObject[];
var InSight : boolean;
var RayIgnore : LayerMask;
var Speed : float = 2;
var EnemyPath : GameObject[];
var ChangeTarDist : float = 1;
private var Player : GameObject;
private var ConnectorScript : ConnectorScript;
private var DistToTarget : float;
private var Target : Transform;
private var PathLength : int;
private var TempStor : GameObject[];
private var i = 0;
private var ArrayNumber : int = 0;
private var DistanceArray : float[];
private var NumberStored : int = 0;
private var GeneratingPath : boolean = true;
private var SmallestDist : float;
function Start () {
Player = GameObject.FindGameObjectWithTag("Player");
}
function Update () {
//Determines if player is in sight
if (!Physics.Linecast(transform.position, Player.transform.position, RayIgnore))
InSight = true;
else
InSight = false;
DeterminePath ();
//Find path length
for (i = 0; i < EnemyPath.Length; i++) {
if (EnemyPath[i] == null) {
PathLength = i;
break;
}
}
//Sets the enemy as the last object in the EnemyPath array
EnemyPath[PathLength] = Player;
//Draws enemy target path lines in editor
if (InSight) {
Debug.DrawLine(transform.position, Player.transform.position, Color.red);
}
else if (PathLength > 1) {
for (i = 0; i < PathLength; i++) {
Debug.DrawLine(EnemyPath[i].transform.position, EnemyPath[i + 1].transform.position, Color.red);
}
}
Move ();
//Empties EnemyPath array
for (i = 0; i < EnemyPath.Length; i++) {
EnemyPath[i] = null;
}
}
function DeterminePath () {
//Sets the player as the first object in the EnemyPath array
EnemyPath[0] = gameObject;
//Only activates if the enemy cannot see the player
if (!InSight) {
//Repeats until it creates a path to connector that can see the player or repeats too many times
for (var f = 1; GeneratingPath == false; i++) {
SmallestDist = Mathf.Infinity;
//Repeats for every connector in the map *Should try to optimize what connectors it looks at
for (i = 0; i < Connectors.Length; i++) {
//Gets the connector script from the current connector
ConnectorScript = Connectors[i].GetComponent("ConnectorScript");
//Finalizes enemy path if the player is in sight of the connector or if the path is longer than the set connectors
if (ConnectorScript.InSight == true || f > 100) {
EnemyPath[f] = Connectors[i];
GeneratingPath = false;
break;
}
//Finds the connector closest to the player and sets that as the next connector in the path
if (ConnectorScript.PlayerDist < SmallestDist) {
SmallestDist = ConnectorScript.PlayerDist;
EnemyPath[f] = Connectors[i];
}
}
}
}
}
function Move () {
//Enemy moves towards player if they are in sight
if (InSight)
transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, Time.deltaTime * Speed);
else
transform.position = Vector3.MoveTowards(transform.position, EnemyPath[1].transform.position, Time.deltaTime * Speed);
}
function SmallestValue (array : float[]) : int {
var SmallestNumber : float = Mathf.Infinity;
for (var i = 0; i < array.Length; i++) {
if (array[i] < SmallestNumber) {
SmallestNumber = array[i];
ArrayNumber = i;
}
}
return ArrayNumber;
}
function EmptyFloatArray (FloatArray : float[]) : float[] {
for (i = 0; i < FloatArray.Length; i++) {
FloatArray[i] = 0;
}
return FloatArray;
}
You need to use Debug.Log to output data to the console during runtime. This way you can see what is going on and selectively debug variables in your script, or simply use it to see if an else statement is being accessed etc.
It probably isn't helping that in the $$anonymous$$ove() function you only change the position of the transform if Insight is true.
I did output a bunch of data through Debug.Log. I deleted it from this script though so it is easier for you guys to see what is happening.
The reason I created a function for moving is because I am planning on adding more code later. That is just the skeleton code so I can see if the pathfinding works.
Ok, so, you say your problem is that your enemy won't move unless your player gets in sight of your enemy.
Considering that, in your $$anonymous$$ove() function you only actually do something if InSight is true, which is only true if your player is in sight of your enemy, that's... not really all that strange. (as maccabbe above me already mentioned).
Also, your enemy seems to just be moving directly towards the player...shouldn't it move towards the next 'connector' on the path? Otherwise, I don't see the point of the pathfinding system you've made?
Your answer
![](https://koobas.hobune.stream/wayback/20220613185227im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pathfinding. Does not "walk" on every tile. 1 Answer
Waypoint System help 1 Answer
Basic Enemy Javascript 0 Answers
Problem with the movement of AI 1 Answer
Overload for method Not Compatible 1 Answer