Advertisement
dronkowitz

AIMover.cs

Nov 30th, 2022
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.AI;
  4. using UnityEngine;
  5.  
  6. [RequireComponent(typeof(NavMeshAgent))]
  7. public class AIMover : MonoBehaviour, IAction
  8. {
  9.     public NavMeshAgent agent;
  10.  
  11.     public PatrolPath path;
  12.     private int pathIndex;
  13.     public float arriveDistance = 1.5f;
  14.  
  15.     private void Start()
  16.     {
  17.         SetDestination(path.GetWayPoint(pathIndex));
  18.     }
  19.  
  20.     public void SetDestination(Vector3 point)
  21.     {
  22.         GetComponent<ActionScheduler>().StartAction(this);
  23.         agent.isStopped = false;
  24.         agent.SetDestination(point);
  25.     }
  26.  
  27.     private void Update()
  28.     {
  29.         float distance = Vector3.Distance(transform.position, agent.destination);
  30.         if (distance < arriveDistance)
  31.         {
  32.             pathIndex = path.GetNextIndex(pathIndex);
  33.             SetDestination(path.GetWayPoint(pathIndex));
  34.         }
  35.     }
  36.  
  37.     public void Cancel()
  38.     {
  39.         agent.isStopped = true;
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement