ZombieAttackAction.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using KairoEngine.Core;
  6. namespace KairoEngine.CharacterSystem
  7. {
  8. /// <summary>
  9. /// Action to move a character in a certain direction and speed.
  10. /// </summary>
  11. public class ZombieAttackAction : IAction
  12. {
  13. CharacterController character; // Reference to the character that will move
  14. Vector3 targetPosition;
  15. string varient;
  16. bool done = false;
  17. float counter = 0f;
  18. bool hit = false;
  19. /// <summary>
  20. /// Action that will make a character follow a target playing a zombie animation.
  21. /// </summary>
  22. /// <param name="character">The CharacterController performing the action.</param>
  23. /// <param name="targetPosition">The target position that the attack will hit.</param>
  24. /// <param name="varient">Use "AttackL", "AttackR" or "Attack".</param>
  25. public ZombieAttackAction(CharacterController character, Vector3 targetPosition, string varient)
  26. {
  27. this.character = character;
  28. this.targetPosition = targetPosition;
  29. this.varient = varient;
  30. this.counter = 0f;
  31. }
  32. public void Start()
  33. {
  34. if(varient == "AttackL") character.animator.SetTrigger("AttackL");
  35. else if(varient == "AttackR") character.animator.SetTrigger("AttackR");
  36. else character.animator.SetTrigger("Attack");
  37. GenericEvents.StartListening(character.unique_name + "-AnimationHit", Hit);
  38. }
  39. public void Update()
  40. {
  41. if(hit)
  42. {
  43. counter += Time.deltaTime;
  44. if(counter > 0.2f)
  45. {
  46. End();
  47. }
  48. }
  49. }
  50. public void Hit()
  51. {
  52. hit = true;
  53. GenericEvents.StopListening(character.unique_name + "-AnimationHit", End);
  54. if(varient == "AttackL") character.leftUnarmedObject.SetActive(true);
  55. else if(varient == "AttackR") character.rightUnarmedObject.SetActive(true);
  56. else
  57. {
  58. character.rightUnarmedObject.SetActive(true);
  59. character.leftUnarmedObject.SetActive(true);
  60. }
  61. }
  62. public void End()
  63. {
  64. done = true;
  65. character.rightUnarmedObject.SetActive(false);
  66. character.leftUnarmedObject.SetActive(false);
  67. }
  68. public bool IsDone()
  69. {
  70. return done;
  71. }
  72. }
  73. }