Making an AI follow a path.
Here's a script that is supposed to have the AI object follow a path by going to a specific point and once it's reached that point, it moves to the next one.
AI ai;
public int current;
public Transform[] path;
void Update() {
if (transform.position != path[current].position) {
ai.TurnToward (path [current]);
ai.MoveForward ();
} else {
current = (current + 1) % path.Length;
}
}
The TurnToward method turns the object towards another object and the MoveFoward object just moves the object forward.
public void MoveForward (){
gameObject.transform.position += gameObject.transform.forward * forwardMovement * Time.deltaTime;
}
public void TurnToward(Transform target) {
transform.LookAt(target);
}
Now when I start the game, the AI moves towards the first position however when it reaches that position, it doesn't recognize that it's reached the position and doesn't update current. So how come the AI doesn't recognize when it's reached the position?
Update: Ok so something weird is happening. I think the reason the position isn't being detected is because the y position of my AI object is going crazy. It constantly changes positions and I have no idea why it's doing that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612112040im_/https://answers.unity.com/themes/thub/images/avi.jpg)