Untitled

 avatar
unknown
csharp
2 months ago
3.6 kB
4
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;

public class NavMeshTargetSetter : MonoBehaviour
{
    private GameManager gameManager;

    [HideInInspector] public ListOfEntities listOfEntities;

    //The target position would be first the explicitly assigned possition, if there are none, it will try to move in range of its aggro focus
    [HideInInspector] public Transform positionTarget;

    //Choose one or the other, BUT NOT BOTH it will use whichever it has assigned
    [Header ("Choose ONE")]
    public PlayerAggro playerAggro;
    public SimplifiedEnemyAggro simplifiedEnemyAggro;

    [HideInInspector] public GeneralBehaviourManager aggroTarget;
    [HideInInspector] public GeneralBehaviourManager friendlyTarget;

    [HideInInspector] public bool areAlliesPriority = false;

    public NavMeshAgent agent;

    private void Awake ()
    {
        gameManager = FindAnyObjectByType<GameManager> ();
    }

    void Start ()
    {

        listOfEntities = GameObject.FindFirstObjectByType<ListOfEntities> ();
        agent = GetComponent<NavMeshAgent> ();

    }

    
    void Update ()
    {

        if (!gameManager.navMesh.isActiveAndEnabled) 
        { 
            return; 
        }

        if (!agent.enabled)
        {
            StartingSetup ();
        }

        aggroTarget = GetAggroTarget ();
        friendlyTarget = GetFriendlyTarget ();

        agent.SetDestination (GetAgentDestination ().position);

    }

    public void StartingSetup ()
    {

        agent.enabled = true;

        agent.updateRotation = false;
        agent.updateUpAxis = false;
        
        SetStoppingDistance ();

    }

    //This method is called to set the stopping distance to the range of the current ability being used so it can reach its target at a preferred range
    //Default value is kinda of an approximation, shouldn't be used, check for possible issues later
    public void SetStoppingDistance (float distance = 1.5f)
    {
        agent.stoppingDistance = distance - (0.1f); //Add small offset to make it slighty closer than max range
    }

    private GeneralBehaviourManager GetAggroTarget ()
    {

        if (playerAggro != null) 
        { 
            return playerAggro.target; 
        }
        else if (simplifiedEnemyAggro != null) 
        { 
            return simplifiedEnemyAggro.aggroTarget; 
        }
        else 
        { 
            return null; 
        } 

    }

    private GeneralBehaviourManager GetFriendlyTarget ()
    {

        if (playerAggro != null) 
        { 
            return playerAggro.friendlyTarget; 
        }
        else if (simplifiedEnemyAggro != null) 
        { 
            return simplifiedEnemyAggro.alliedTarget; 
        }
        else 
        { 
            return null; 
        }

    }

    private Transform GetAgentDestination ()
    {

        if (positionTarget != null) 
        { 
            return positionTarget; 
        }
        else if (friendlyTarget != null && areAlliesPriority) 
        { 
            return friendlyTarget.transform; 
        }
        else if (aggroTarget != null) 
        { 
            return aggroTarget.transform;
        }
        else if (friendlyTarget != null) 
        { 
            return friendlyTarget.transform; 
        }
        else 
        {
            return null;
        }

    }

}
Editor is loading...
Leave a Comment