Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Enemy : MonoBehaviour {
- public float speed = 10f;
- private Transform target;
- private int wavepointIndex = 0;
- void Start()
- {
- target = Waypoints.points[0];
- }
- void Update()
- {
- 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 >= Waypoints.points.Length - 1)
- {
- EndPath();
- return;
- }
- wavepointIndex++;
- target = Waypoints.points[wavepointIndex];
- }
- void EndPath()
- {
- PlayerStats.Lives--;
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement