- Home /
Performing NPC movement with a function.
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?
Hi, Is this the complete script and are you calling the patrolling function
No, this is just the function, the complete script is just this function, but it is called directly in the Update() function. When I use it in an outside function, it does not increment currentWaypoint. If I do this function with individual lines inside Update(), it works fine.
Answer by deltamish · Feb 18, 2013 at 06:48 AM
Hi, Dont use moveDirectionAI.magnitude < .1 This returns higher value as it computes direction and converts it int scalar quantity use Vector3.distance or just change .1 to some higher value(approx 6)
I tried to increase the value, but my character simply stops when he gets to his first waypoint. I tried with Vector3.Distance, but I get an error: Operator < cannot be applied to operands of type 'method group' and 'int'.
Answer by Loius · Feb 18, 2013 at 09:45 PM
Functions pass by value (by default). You're changing only your local copy of currentWaypointAI.
You can either pass by ref:
Update() { int argument = 0; Function( ref argument ); Debug.Log(argument); }
Function(ref arg) { arg++; }
Or declare your variable as a class member and don't pass it at all.
but if i set currentwaypointAI to zero everytime in update, wouldn't the currentwaypointAI always be zero?
Also, I eventually want to be able to use my patrolling function in many different AI scripts. To that end, I want to be able to pass it a waypoint array, movement speed, and an NPC to move, and have it work its mighty magic.
$$anonymous$$y example was an example; if you run that you'll see that argument is changed inside Function and that it "remains changed" inside Update (it'll print 1, not 0), which is the behaviour you're asking for, ya?
I'd highly recommend making the current waypoint (and much of that other info like speed and friends) a member variable of the NPC class that you pass into the function. If you can say "This NPC has a speed", speed should be a member variable. (And "This NPC has a X" means X should be a member var)
Your answer
![](https://koobas.hobune.stream/wayback/20220613092911im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Navmesh Enemy AI, Syntax issue? 1 Answer
WayPoints mixed with Raycast 1 Answer
Way Point Script Change Group 0 Answers
Why isn't a Ai Waypoint Spawning??? 0 Answers
Confused about copying Lists! 1 Answer