Newer
Older
HierarchicalFSM-Unity3D / Assets / Scripts / NPC / FSM / Actions / Combat / Ranged / EngagePlayerAction.cs
@Rackday Rackday on 1 Aug 1 KB Enemy
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;

[CreateAssetMenu(menuName = "Finite State Machine/Action/Combat/Ranged/EngagePlayer")]
public class EngagePlayerAction : Action
{
    [SerializeField] private LookAt lookAt;
    [SerializeField] private float targetDistance = 2.0f;
    public override void Execute(FSM fsm)
    {
        NavMeshAgent agent = fsm.Controller.Agent;
        GameObject target = fsm.Controller.AgentFOV.GetTarget();
        Animator anim = fsm.Controller.Animator;

        //If the agent has a target
        if (target != null)
        {
            lookAt.SetTarget(target);
            lookAt.Execute(fsm);

            //Sets the combat locomotion for the enemy
            anim.SetBool("InCombat", true);
            agent.stoppingDistance = targetDistance;

            if (agent.stoppingDistance >= targetDistance)
            {
                agent.destination = target.transform.position;
            }
        }
    }
}