RemoveBody.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. using KairoEngine.Utilities;
  7. namespace KairoEngine.CharacterSystem
  8. {
  9. public class RemoveBody : MonoBehaviour
  10. {
  11. public float waitTime = 30f;
  12. public float animTime = 3f;
  13. private CharacterController character;
  14. private bool removing = false;
  15. private bool isAnimating = false;
  16. private Vector3 startPosition;
  17. private Vector3 endPosition;
  18. private float counter = 0f;
  19. void Start()
  20. {
  21. character = GetComponent<CharacterController>();
  22. if(character == null) Debug.LogError("Could not find a CharacterController component.", this.gameObject);
  23. }
  24. void Update()
  25. {
  26. if(character == null) return;
  27. if(character.IsDead() && !removing) StartTimer();
  28. if(isAnimating) Animate();
  29. }
  30. void StartTimer()
  31. {
  32. removing = true;
  33. character.puppetMaster.gameObject.SetActive(false);
  34. StartCoroutine(Timer.Start(waitTime, false, () =>
  35. {
  36. startPosition = transform.position;
  37. endPosition = startPosition;
  38. endPosition.y -= 1f;
  39. isAnimating = true;
  40. character.RemoveColliders();
  41. }));
  42. }
  43. void Animate()
  44. {
  45. counter += Time.deltaTime;
  46. Vector3 pos = startPosition;
  47. float y = Mathf.Lerp(startPosition.y, endPosition.y, counter/animTime);
  48. pos.y = y;
  49. transform.position = pos;
  50. if(counter > animTime)
  51. {
  52. isAnimating = false;
  53. RemoveCharacter();
  54. }
  55. }
  56. void RemoveCharacter()
  57. {
  58. Destroy(character.transform.root.gameObject);
  59. }
  60. }
  61. }