96. Move Enemy On Path - Check if we've reached the target point?

Hello there, I’m very confused about this part of the operation - checking if we’ve reached the target point, if we have, increase the index. I have provided the pseudo code also the actual implementation below. So why are we doing this checking? Why will it not work if we just ignore this if statement and just increase index in each frame? We’re checking if the movement has been done successfully, doesn’t that imply that there’s a chance that the Vector2.MoveTowards method won’t work? How’s that possible?


Hi Tim,

Our waypoints are stored in an array. waypointIndex tells Unity to which waypoint we want to move. We do not increment the value each frame because our enemy is not supposed not move to another waypoint each frame. It is supposed to move to the “current” waypoint. Once it reached that “current” waypoint, it is supposed to move to the “next” one, which then becomes our “current” target waypoint.

The computer has no conception of “current” or “next”. That’s why we need a variable, here: waypointIndex, to define “current”.

Did this clear it up for you?


See also:

Yes, thank you Nina.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms