Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / AI / FSM / Actions / AttackAction.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [CreateAssetMenu(menuName = "Finite State Machine/Action/AttackMelee")]
  5. public class AttackAction : Action
  6. {
  7. [SerializeField] private float damage;
  8. [SerializeField] private float distance;
  9. [SerializeField] private float timerTotal;
  10. RaycastHit ray;
  11. Health hp = null;
  12. public override void Act(FiniteStateMachine fsm)
  13. {
  14. if (fsm.floatDictionary.GetValue("coolDown", 0) > 0)
  15. {
  16. fsm.floatDictionary.SetValue("coolDown", fsm.floatDictionary.GetValue("coolDown") - Time.deltaTime);
  17. }
  18. else
  19. {
  20. if (fsm.target != null)
  21. {
  22. fsm.transform.LookAt(fsm.target.transform.position);
  23. fsm.rb.velocity = Vector3.zero;
  24. if (Physics.Raycast(fsm.transform.position, fsm.transform.forward, out ray, Vector3.Distance(fsm.transform.position, fsm.target.transform.position)))
  25. {
  26. if (hp == null) ray.collider.gameObject.TryGetComponent<Health>(out hp);
  27. else
  28. {
  29. hp.LooseHp(damage, fsm.transform.forward);
  30. }
  31. }
  32. fsm.floatDictionary.SetValue("coolDown", timerTotal);
  33. }
  34. }
  35. }
  36. }