I am working on a function for a patrolling NPC’s, but I’ve run into some issues. When I had the code in my Update() block, it worked fine, but now it is not updating key variables, and I am sure it is just because I’m not making use of the variables persistently. Here’s some code:
void patrolling(Transform[] waypointAI, int currentWaypointAI, Vector3 targetAI, Vector3 moveDirectionAI, Vector3 velocityAI, float speedAI) {
wpLengthGUI.text = waypointAI.Length.ToString();
methodStateGUI.text = "Method Started";
methodCWP.text = currentWaypointAI.ToString();
if (currentWaypointAI < waypointAI.Length) {
methodStateGUI.text = "Inside first IF";
targetAI = waypointAI[currentWaypointAI].position;
moveDirectionAI = targetAI - transform.position;
velocityAI = rigidbody.velocity;
} else {
methodStateGUI.text = "Inside first ELSE";
currentWaypointAI = -1;
}
if (moveDirectionAI.magnitude < .1) {
methodStateGUI.text = "Inside second IF";
currentWaypointAI = currentWaypointAI ++;
} else {
methodStateGUI.text = "Inside second ELSE";
velocityAI = moveDirectionAI.normalized * speed;
}
//methodStateGUI.text = "Outside IFs, end ";
rigidbody.velocity = velocityAI;
}
What happens when I run this as a callable function is that currentWaypointAI is never incremented, even though my NPC is clearly less than .1 units away. Anyone have any insight into this?