Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EnemyPathing : MonoBehaviour
- {
- // new serialized field to drop the root prefab into
- [SerializeField] Transform path;
- // Doesn't need to, and really shouldn't be serialized any more
- [SerializeField] float moveSpeed = 2f;
- List<Transform> waypoints = new List<Transform>();
- int waypointIndex = 0;
- void Start()
- {
- // Simple iteration of the children transforms and adding them to the list
- // if they're not in order (0), (1), (2) in the hierarchy, they wont be in the list, either.
- for (int i = 0; i < path.childCount; i++)
- {
- waypoints.Add(waypoints[i]);
- }
- transform.position = waypoints[waypointIndex].position;
- }
- private void Move()
- {
- if (waypointIndex <= waypoints.Count - 1)
- {
- var targetPosition = waypoints[waypointIndex].transform.position;
- var movementThisFrame = moveSpeed * Time.deltaTime;
- transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame);
- if (transform.position == targetPosition)
- {
- waypointIndex++;
- }
- }
- else
- {
- Destroy(gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement