One script stopped working after update
I upgraded to unity 2017.2.0 from some version of 5; I don't remember the specifics, but I digress. I was working on a 2D project before the update, and now everything seems to work okay except for one thing: my pathfinding script. I have no idea what's causing it to bug up, but from what I can tell, it's not really in my control, huh? There hasn't been a change to the script since I first typed it up.
So let me get into the specific problem here. This script works in conjunction with a map of waypoints evenly spaced on the map. The AI is trying to get to the player, that's their main target. If the player is in sight, the AI is supposed to just go directly to them, however, if the player is obstructed, the AI uses the waypoint system to try to get to the player in the most efficient manner.
I'll paste it here if anyone wants to take a look and see if they can find anything wrong with it. It's not the best out there, but it used to get the job done.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pathFinderScript : MonoBehaviour {
public GameObject compass; //compass to instantiate if one doesn't exist, points AI in the direction to go
[HideInInspector] public GameObject myCompass; //compass that was instantiated
aiControllerScript myController; //this object's controller script
public Transform target; //current target
public Transform ultTarget; // the target we are trying to ultimately reach
float axis = 1.0f;
public LayerMask layerMask; //used to find new targets
bool changedTarget;
//Lists for the open and closed waypoints
List<GameObject> openPoints = new List<GameObject>(); //waypoints we can target
List<GameObject> closedPoints = new List<GameObject>(); //waypoints we have already targeted
//function to find the waypoint closest to the midpoint between this and the ultTarget that is unobstructed
Transform findWaypoint(){
float prevDistance = Mathf.Infinity; // Distance for comparison; starts at infinity since we have no target yet
Transform tempTarget = ultTarget;
// Find the target waypoint
for (int i = 0; i < openPoints.Count; i++) {
if (!Physics2D.Linecast (transform.position, openPoints [i].transform.position, layerMask) &&
(Vector2.Distance (ultTarget.transform.position, openPoints [i].transform.position) + Vector2.Distance(transform.position,openPoints [i].transform.position) < prevDistance &&
Vector2.Distance (transform.position, openPoints [i].transform.position) > 2.0f)) {
prevDistance = Vector2.Distance (transform.position, openPoints [i].transform.position) + Vector2.Distance(ultTarget.position, openPoints [i].transform.position);
tempTarget = openPoints [i].transform;
}
}
// Set our actual target to the newfound target
target = tempTarget;
return target;
}
void resetLists(){
//first, put every waypoint into an array
GameObject[] waypoints = GameObject.FindGameObjectsWithTag ("waypoint");
//clear the openPoints list
openPoints.Clear();
//add every waypoint to the open list
foreach(GameObject waypoint in waypoints){
openPoints.Add (waypoint);
}
//clear the closed list
closedPoints.Clear();
}
// Use this for initialization
void Start () {
resetLists ();
//get ultTarget
ultTarget = GameObject.FindWithTag("Player").transform;
//instantiate a compass for this object to use
myCompass = Instantiate(compass,transform.position, transform.rotation);
//get this object's controller
myController = GetComponent<aiControllerScript>();
findWaypoint ();
}
// Update is called once per frame
void Update () {
//update the compass's position and rotation
myCompass.transform.position = transform.position;
float AngleRad = Mathf.Atan2 (target.transform.position.y - myCompass.transform.position.y, target.transform.position.x - myCompass.transform.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
myCompass.GetComponent<Rigidbody2D>().rotation = angle;
//temporarily store values in an array to pass to the move function in the controller
object[] tempStorage = new object[2];
tempStorage [0] = axis;
tempStorage [1] = myCompass;
//move the direction the compass is facing
myController.SendMessage("move",tempStorage);
//target handling
//cast a line to the ultTarget to see if there is an obstruction in the way
if (Physics2D.Linecast (transform.position, ultTarget.position, layerMask)) {
//find the next waypoint in the path if we have reached the previous one
if (target != ultTarget && Vector2.Distance (transform.position, target.position) < 1.0f) {
openPoints.Remove(target.gameObject);
closedPoints.Add (target.gameObject);
findWaypoint ();
} else if (target == ultTarget) {
findWaypoint ();
}
} else {
//otherwise, keep target as the ultTarget and reset the lists
target = ultTarget;
resetLists ();
}
//if there are a lot of closed points, reset them so that we can go to those points again if we need to
if (closedPoints.Count >= 20) {
resetLists ();
}
}
}
Your answer
Follow this Question
Related Questions
Player keeps bumping when walking over to another sprite 1 Answer
How to detect if floor exists using Raycast 1 Answer
How can make it so only 1 object of type “Ability” can be selected at once? 0 Answers
Attach a vehicle to multiple paths? 0 Answers
HingeJoint2D(and maybe other joints in 2D) Not properly Rotating. Let us Solve this Together 0 Answers