Advertisement
Guest User

Untitled

a guest
Aug 19th, 2016
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Enemy : MonoBehaviour
  5. {
  6.     public float speed = 10f;
  7.     private Transform target;
  8.     private int wavePointIndex = 0;
  9.  
  10.     Waypoint wayPoint;
  11.  
  12.     // Use this for initialization
  13.     void Start()
  14.     {
  15.         GameObject tempObj = GameObject.Find("Waypoints");
  16.         wayPoint = tempObj.GetComponent<Waypoint>();
  17.  
  18.         if (wayPoint.points != null)
  19.         {
  20.             target = wayPoint.points[0];
  21.         }
  22.         else
  23.         {
  24.             wayPoint.initPoints();
  25.             target = wayPoint.points[0];
  26.         }
  27.         // An error pops up on the first frame for this line of code below
  28.     }
  29.  
  30.     // Update is called once per frame
  31.     void Update()
  32.     {
  33.         // This is the main source of the error below.
  34.         Vector3 dir = target.position - transform.position;
  35.         transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
  36.  
  37.         if (Vector3.Distance(transform.position, target.position) <= 0.4f)
  38.         {
  39.             GetNextWayPoint();
  40.         }
  41.     }
  42.  
  43.     void GetNextWayPoint()
  44.     {
  45.         if (wavePointIndex >= wayPoint.points.Length - 1)
  46.         {
  47.             Destroy(gameObject);
  48.             return;
  49.         }
  50.         wavePointIndex++;
  51.         target = wayPoint.points[wavePointIndex];
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement