Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine.AI;
- using UnityEngine;
- using System.Collections;
- using System.Linq;
- public enum AgentType
- {
- None = 0,
- Player,
- Enemy
- }
- public interface IAgent
- {
- AgentType AgentType { get; }
- GameObject gameObject { get; }
- Vector3 Position { get; }
- }
- public struct AgentHitPair
- {
- public IAgent Agent1;
- public IAgent Agent2;
- }
- public class FindNearestEnemy : MonoBehaviour
- {
- public static FindNearestEnemy instance;
- private Teamup team;
- private List<GameObject> Enemies;
- public float DistanceBetweenAgents;
- public bool Searching { get; private set; }
- private List<IAgent> Agents;
- private List<AgentHitPair> AgentHits;
- private float DistanceBetweenAgentsSquare;
- public bool EnemySearched = false;
- private NavMeshAgent nma;
- public bool NearestTarget = false;
- private void Awake ( )
- {
- team = GetComponent<Teamup> ( );
- nma = GetComponent<NavMeshAgent> ( );
- if ( instance == null )
- {
- instance = this;
- }
- }
- private void Start ( )
- {
- FindEnemies ( );
- }
- public void FindEnemies ( )
- {
- Agents = new List<IAgent> ( );
- var Allenemies = FindObjectsOfType<Teamup> ( );
- string nametolookfor = GetComponent<Teamup> ( ).OppositeTeamTag;
- for ( int i = 0; i < Allenemies.Length; i++ )
- {
- if ( Allenemies [ i ].gameObject.GetComponent<Teamup> ( ).OppositeTeamTag == nametolookfor )
- {
- Agents.Add ( ( IAgent ) Allenemies [ i ] );
- }
- }
- AgentHits = new List<AgentHitPair> ( );
- DistanceBetweenAgentsSquare = DistanceBetweenAgents * DistanceBetweenAgents;
- Searching = true;
- Search ( );
- }
- private async void Search ( )
- {
- do
- {
- AgentHits.Clear ( );
- // Do a brute force distance check between each agent.
- for ( int i = 0; i < Agents.Count; i++ )
- {
- for ( int j = 0; j < Agents.Count; j++ )
- {
- if ( Agents [ i ] == Agents [ j ] )
- continue;
- var magnitude = ( Agents [ i ].Position - Agents [ j ].Position ).sqrMagnitude;
- if ( magnitude <= DistanceBetweenAgentsSquare )
- {
- // The two agents are closer than DistanceBetweenAgentsSquare distance to each other.
- AgentHits.Add ( new AgentHitPair { Agent1 = Agents [ i ], Agent2 = Agents [ j ] } );
- }
- }
- }
- // AgentHits now holds the Agents that are "close" to each other.
- if ( AgentHits.Count > 0 )
- {
- StringBuilder output = new StringBuilder ( );
- for ( int i = 0; i < AgentHits.Count; i++ )
- {
- output.AppendLine ( $"AgentHits : {AgentHits [ i ].Agent1.gameObject.name} [{AgentHits [ i ].Agent1.AgentType}] is close to {AgentHits [ i ].Agent2.gameObject.name} [{AgentHits [ i ].Agent2.AgentType}]." );
- EnemySearched = true;
- Vector3 dir = AgentHits [ i ].Agent2.gameObject.transform.position - this.transform.position;
- dir.y = 0;
- //nma.destination = AgentHits[i].Agent2.GameObject.transform.position;
- GetComponent<Teamup> ( ).Target = AgentHits [ i ].Agent2.gameObject.transform;
- NearestTarget = true;
- }
- }
- await Task.Delay ( 1000 );
- }
- while ( Searching );
- }
- private void OnDisable ( )
- {
- Searching = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment