aboutsummaryrefslogtreecommitdiff
path: root/td/Assets/Scripts/Enemy.cs
blob: 7273370e295aa93648425e618ba29e7e15ae72c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour {

	public float speed;
	public float initialHp;
	public Transform pathWay;

	List<Vector3> waypoints = new List<Vector3>();
	Vector3 waypointPos;
	int waypointNum = -1;  // Using minus one so that first addition returns 0, first element in array

	void Start () {
		foreach (Transform child in pathWay) {
			waypoints.Add (child.position);
		}
	}

	void Update () {
		updateWaypoint ();

		float transformStep = speed * Time.deltaTime;
		transform.position = Vector3.MoveTowards (transform.position, waypointPos, transformStep);

		if (waypointNum == waypoints.Count - 1) {
			Destroy (gameObject);
		}
	}

	void updateWaypoint() {
		if ( (transform.position == waypointPos && waypointNum < waypoints.Count - 1) || waypointNum == -1) {
			waypointNum++;
			waypointPos = new Vector3 (waypoints [waypointNum].x, 0.604f, waypoints [waypointNum].z);
		}
	}
		
}