Advertisement
The3vilM0nk3y

Untitled

Oct 17th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using UnityEngine.Networking;
  6.  
  7. public class EnemyPathing : NetworkBehaviour {
  8.  
  9.     public double attackRange = 5.0;
  10.     public float pathingUpdateTime = 0.5f;
  11.    
  12.     public NavMeshAgent unitAgent;
  13.  
  14.     private GameObject[] targets;
  15.     [SyncVar]
  16.     private GameObject target;
  17.     [SyncVar]
  18.     private NavMeshPath path;
  19.     private float pathingCooldown = 0.0f;
  20.  
  21.     // Use this for initialization
  22.     void Start () {
  23.         path = new NavMeshPath();
  24.     }
  25.    
  26.     // Update is called once per frame
  27.     void Update () {
  28.         //Pathfinding
  29.        
  30.         DoPathing();
  31.            
  32.     }
  33.     private void DoPathing()
  34.     {
  35.         pathingCooldown += Time.deltaTime;
  36.         if (pathingCooldown > pathingUpdateTime)
  37.         {
  38.             pathingCooldown -= pathingUpdateTime;
  39.            
  40.             target = FindNewTarget();
  41.            
  42.             if (target != null)
  43.             {
  44.                 //check if target is in attack range if so attack.
  45.                 float d = GetDistance(target.transform.position);
  46.                 double dist = d;
  47.                 //print("Distance from target" + dist);
  48.                 if (dist <= attackRange)
  49.                 {
  50.                     //attack mechanics done in different timer in update loop.
  51.                     //stop moving to target once in range
  52.                     if (unitAgent.hasPath)
  53.                     {
  54.                         unitAgent.ResetPath();
  55.                     }
  56.                     return;
  57.                 }
  58.                 //if target is not in range, move closer to the target.
  59.                 NavMesh.CalculatePath(unitAgent.transform.position, target.transform.position, NavMesh.AllAreas, path);
  60.                 unitAgent.path = path;
  61.                 unitAgent.destination = target.transform.position;
  62.                     //if so move to target
  63.                     //if not find new target
  64.                         //check tracking range for new target
  65.                         //if no target found, move back to spawn point
  66.  
  67.             }
  68.         }
  69.  
  70.     }
  71.     public GameObject FindNewTarget()
  72.     {
  73.         //print("Aquiriring Targets");
  74.         targets = GameObject.FindGameObjectsWithTag("Player Hero");
  75.        
  76.         //check if it found any targets.
  77.         if (targets.Length > 0)
  78.         {
  79.             if (targets.Length == 1)
  80.             {
  81.                 //print("Only one target.");
  82.                 return targets[0];
  83.             }
  84.             //print("Multiple targets found.");
  85.             List<float> distances = new List<float>();
  86.             List<double> weights = new List<double>();
  87.             foreach (GameObject t in targets)
  88.             {
  89.                 //calculate the distance to the target and save it temporarily.
  90.                 float testDistance = GetDistance(t.transform.position);
  91.                 distances.Add(testDistance);
  92.                 //calculate and save the weight value.
  93.                 weights.Add(testDistance / t.GetComponentInParent<PathingData>().pathingWeight);
  94.             }
  95.             int bestIndex = 0;
  96.             for (int i = 0; i < targets.Length; i++)
  97.             {
  98.                 if (weights[i] < weights[bestIndex])
  99.                 {
  100.                     bestIndex = i;
  101.                 }
  102.                 else if (weights[i] == weights[bestIndex])
  103.                 {
  104.                     if (distances[i] < distances[bestIndex])
  105.                     {
  106.                         bestIndex = i;
  107.  
  108.                     }
  109.                 }
  110.             }
  111.             return targets[bestIndex];
  112.  
  113.         } else {
  114.             print("ALL PLAYERS DEAD?!?!?!");
  115.             return null;
  116.         }
  117.     }
  118.     public float GetDistance(Vector3 t)
  119.     {
  120.         NavMesh.CalculatePath(unitAgent.transform.position, t, NavMesh.AllAreas, path);
  121.         float dist = 0.0f;
  122.         for (int i = 0; i < path.corners.Length - 1; i++)
  123.             dist += Vector3.Distance(path.corners[i], path.corners[i + 1]);
  124.         return dist;
  125.        
  126.        
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement