Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / AI / FSM / Actions / AttackAction.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Finite State Machine/Action/AttackMelee")]
public class AttackAction : Action
{
    [SerializeField] private float damage; 
    [SerializeField] private float distance;
    [SerializeField] private float timerTotal;
    RaycastHit ray;
    Health hp = null;
    
    public override void Act(FiniteStateMachine fsm)
    {
       
        if (fsm.floatDictionary.GetValue("coolDown", 0) > 0)
        {
            fsm.floatDictionary.SetValue("coolDown", fsm.floatDictionary.GetValue("coolDown") - Time.deltaTime);
        }
        else
            {
            if (fsm.target != null)
            {
                fsm.transform.LookAt(fsm.target.transform.position);
                fsm.rb.velocity = Vector3.zero;
                if (Physics.Raycast(fsm.transform.position, fsm.transform.forward, out ray, Vector3.Distance(fsm.transform.position, fsm.target.transform.position)))
                {
                    
                    if (hp == null) ray.collider.gameObject.TryGetComponent<Health>(out hp);
                    else
                    {
                        hp.LooseHp(damage, fsm.transform.forward);
                    }
                }
                fsm.floatDictionary.SetValue("coolDown", timerTotal);
            }
        }   
    }
}