Hello, I just started doing tower defense game and as first I am trying to do enemy movement. But the thing is, that enemies after instantiate skip first point to move and goes straight to 2nd, then to 3rd as they should. But can’t figure out why they skip that first point…
Here is the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyMovement : MonoBehaviour {
public float movementSpeed;
public List<Transform> movePoints;
private int _index;
private Vector3 _moveDirection;
void Start() {
GameObject[] pointsToMove = GameObject.FindGameObjectsWithTag("MovePoint");
foreach(GameObject ptm in pointsToMove) {
movePoints.Add(ptm.transform);
}
_index = 0;
_moveDirection = (movePoints[_index].position - transform.position).normalized;
}
void FixedUpdate() {
transform.position += _moveDirection * movementSpeed * Time.deltaTime;
}
void OnTriggerEnter(Collider other) {
if(other.tag == "MovePoint") {
_index++;
if(_index == movePoints.Count-1) {
Destroy(gameObject);
}
else {
_moveDirection = (movePoints[_index].position - transform.position).normalized;
}
}
}
}
And here is a picture: