Advertisement
Guest User

Custom AIPath controller

a guest
Mar 25th, 2018
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using UnityEngine;
  2. using BehaviorDesigner.Runtime;
  3. using Pathfinding;
  4. using Gameplay.Agents;
  5. using Gameplay.Agents.Controllers;
  6.  
  7. namespace Gameplay.AI {
  8.     public class NPCPath : MonoBehaviour {
  9.         public BehaviorDesigner.Runtime.BehaviorTree BehaviorTree;
  10.         public AIPath AIPath;
  11.         public NPCController NPCController;
  12.  
  13.         AgentBase Agent { get { return NPCController.Agent; } }
  14.  
  15.         SharedBool _isRunning;
  16.         bool IsRunning {
  17.             get {
  18.                 if (_isRunning == null) { _isRunning = (SharedBool)BehaviorTree.GetVariable("IsRunning"); }
  19.                 return _isRunning.Value;
  20.             }
  21.         }
  22.  
  23.         Vector3 _desiredPosition;
  24.         Quaternion _desiredRotation;
  25.  
  26.         void Update() {
  27.             ResetAIPath();
  28.         }
  29.  
  30.         void FixedUpdate() {
  31.             ResetAIPath();
  32.  
  33.             if (GameplayManager.IsPaused) { return; }
  34.  
  35.             UpdateMovement();
  36.         }
  37.  
  38.         void ResetAIPath() {
  39.             AIPath.canMove = AIPath.updatePosition = AIPath.updateRotation = false;
  40.             AIPath.rvoController = NPCController.AgentBehaviour.RVOController;
  41.         }
  42.  
  43.         void UpdateMovement() {
  44.             if (NPCController.State == NPCState.Dead) {
  45.                 Agent.MoveDelta(Vector2.zero);
  46.  
  47.                 return;
  48.             }
  49.  
  50.             AIPath.maxSpeed = Agent.GetMoveSpeed(Agent.Health, IsRunning);
  51.  
  52.             AIPath.MovementUpdate(Time.deltaTime, out _desiredPosition, out _desiredRotation);
  53.  
  54.             Agent.MoveDelta((Vector2)_desiredPosition - Agent.Position);
  55.  
  56.             AIPath.FinalizeMovement(Agent.Position, Quaternion.identity);
  57.  
  58.             Agent.Position = AIPath.position; // Update Agent position in case any external forces were applied during FinalizeMovement.
  59.         }
  60.  
  61.         public void Stop() {
  62.             AIPath.isStopped = true;
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement