Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Enemy : MonoBehaviour
- {
- public float speed = 10f;
- private Transform target;
- private int wavePointIndex = 0;
- Waypoint wayPoint;
- // Use this for initialization
- void Start()
- {
- GameObject tempObj = GameObject.Find("Waypoints");
- wayPoint = tempObj.GetComponent<Waypoint>();
- if (wayPoint.points != null)
- {
- target = wayPoint.points[0];
- }
- else
- {
- wayPoint.initPoints();
- target = wayPoint.points[0];
- }
- // An error pops up on the first frame for this line of code below
- }
- // Update is called once per frame
- void Update()
- {
- // This is the main source of the error below.
- Vector3 dir = target.position - transform.position;
- transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
- if (Vector3.Distance(transform.position, target.position) <= 0.4f)
- {
- GetNextWayPoint();
- }
- }
- void GetNextWayPoint()
- {
- if (wavePointIndex >= wayPoint.points.Length - 1)
- {
- Destroy(gameObject);
- return;
- }
- wavePointIndex++;
- target = wayPoint.points[wavePointIndex];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement