First, I'm going to start off saying that I know that this isn't the most efficient or best pathfinding system, I just wanted to try and create my own before looking at some of the better ways of doing it. Now that that's out of the way I'm just going to briefly explain how the script works. The script is designed for building and works through the use of things I call connectors. These are just empty gameobjects placed in strategic places around the map such as doorways or in the middle of rooms. Each connector measures the distance between it and the player and enemy, and also checks to see whether or not they are in view.
The script tries to determine a path through the following steps:
The enemy looks goes through a list of connectors and checks to see whether they can see the enemy (I'm going to optimize this later on so that if a connector is in sight it will but itself to the list the enemy checks)
It then checks to see if the connector can see the player. If it can it finishes the path
If it can't see the player, it stores how close the connector is from the player, and uses that info to pick the next connector in the path
It repeats until one of the connectors sees the player or it goes over the path limit.
I tried to put a ton of notes on this script so it is more understandable. I am relatively new to coding so I know that you could probably write a book about all of the inefficiencies in this code but please try to keep any tips simple. Easy things to do that are good to get in a habit of and that you could explain to a toddler. It seems to me that one of the most likely reasons it is crashing is because of an infinity repeating for loop but I can't seem to see any problems with them. Thanks for taking the time to read this and look over my code. Any advice for how to improve the code or fix other problems in it would be greatly appreciated.
#pragma strict
var Connectors : GameObject[];
var InSight : boolean;
var RayIgnore : LayerMask;
var Speed : float = 2;
private var PathLength : int;
private var EnemyPath : GameObject[] = new GameObject[10];
private var Player : GameObject;
private var ConnectorScript : ConnectorScript;
private var i = 0;
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 final object in the path to the enemy
EnemyPath[PathLength] = gameObject;
//Draws enemy target path lines in editor
if (InSight) {
Debug.DrawLine(transform.position, Player.transform.position, Color.red);
}
else {
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] = Player;
//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; i++) {
SmallestDist = Mathf.Infinity;
//Number of connectors in enemy path
if (f > 6)
GeneratingPath = false;
//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");
Debug.Log("Test 1");
//Checks to see if the connector can see the previous position on the enemy path
if (!Physics.Linecast(Connectors[i].transform.position, EnemyPath[PathLength + 1].transform.position, RayIgnore)) {
//Finalizes enemy path if the player is in sight of the connector
if (ConnectorScript.InSight == true) {
Debug.Log("Test 2");
EnemyPath[f] = Connectors[i];
GeneratingPath = false;
break;
}
//Finds the connector closest to the player and sets that as the next connector in the path
else (ConnectorScript.PlayerDist < SmallestDist) {
Debug.Log("Test 3");
SmallestDist = ConnectorScript.PlayerDist;
EnemyPath[f] = Connectors[i];
}
}
}
}
EnemyPath[PathLength] = gameObject;
GeneratingPath = true;
}
}
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[PathLength - 1].transform.position, Time.deltaTime * Speed);
}
↧