DamageEmitter.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. using KairoEngine.SFX;
  7. using KairoEngine.Inventory;
  8. namespace KairoEngine.CharacterSystem
  9. {
  10. public class DamageEmitter : MonoBehaviour
  11. {
  12. public string title = "Damager";
  13. public int damage = 1;
  14. public int armorPiercing = 0;
  15. public float attackTime = 0.8f;
  16. public CharacterController character;
  17. public SFXGroup hitSFXGroup;
  18. private CharacterController damagingTarget;
  19. private float counter = 0f;
  20. void Update()
  21. {
  22. if(damagingTarget != null)
  23. {
  24. counter += Time.deltaTime;
  25. if(counter > attackTime)
  26. {
  27. counter = 0f;
  28. damagingTarget = null;
  29. }
  30. }
  31. }
  32. public void OnTriggerEnter(Collider collider)
  33. {
  34. if(this.enabled == false) return;
  35. //Debug.Log("Damaging " + collider.gameObject.name);
  36. Vector3 point = new Vector3();
  37. point = collider.ClosestPoint(transform.position);
  38. CharacterController targetCharacter = collider.gameObject.transform.root.GetComponent<WorldObject>()?.character;
  39. if(targetCharacter != null)
  40. {
  41. if(targetCharacter == character || targetCharacter == damagingTarget) return;
  42. if(damagingTarget == null) damagingTarget = targetCharacter;
  43. if(targetCharacter.damageController != null)
  44. {
  45. targetCharacter.damageController.Damage(this, point);
  46. }
  47. }
  48. if(collider.material != null) hitSFXGroup.Play(collider.material, point);
  49. }
  50. }
  51. }