Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Unit : MonoBehaviour {
- public Transform target;
- float speed = 20;
- Vector2[] path;
- int targetIndex;
- void Start()
- {
- PathrequestManager.RequestPath(transform.position, target.position, OnPathFound);
- }
- public void OnPathFound(Vector2[] newPath, bool pathSuccessful)
- {
- if (pathSuccessful)
- {
- path = newPath;
- targetIndex = 0;
- StopCoroutine("FollowPath");
- StartCoroutine("FollowPath");
- }
- }
- IEnumerator FollowPath()
- {
- Vector2 currentWaypoint = path[0];
- while (true)
- {
- if (transform.position == currentWaypoint)
- {
- targetIndex++;
- if (targetIndex >= path.Length)
- {
- yield break;
- }
- currentWaypoint = path[targetIndex];
- }
- transform.position = Vector2.MoveTowards(transform.position, currentWaypoint, speed * Time.deltaTime);
- yield return null;
- }
- }
- public void OnDrawGizmos()
- {
- if (path != null)
- {
- for (int i = targetIndex; i < path.Length; i++)
- {
- Gizmos.color = Color.black;
- Gizmos.DrawCube(path[i], Vector3.one);
- if (i == targetIndex)
- {
- Gizmos.DrawLine(transform.position, path[i]);
- }
- else
- {
- Gizmos.DrawLine(path[i - 1], path[i]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement