Soul.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. using uTools;
  6. public class Soul : MonoBehaviour
  7. {
  8. public Rigidbody rb;
  9. public bool collected = false;
  10. public float lerpValue = 0.05f;
  11. public int addMp = 10;
  12. public uTweenPositionTarget tweenPos;
  13. public float flySpeed = 10f;
  14. public float upSpeed = 1;
  15. public float shootSpeed;
  16. public float altitude;
  17. [HideInInspector]
  18. public bool isShoot;
  19. [HideInInspector]
  20. public Vector3 from;
  21. [HideInInspector]
  22. public Vector3 to;
  23. [HideInInspector]
  24. public float time;
  25. private void Awake()
  26. {
  27. SoulInMap.souls.Add(this);
  28. }
  29. public void Burst(Vector3 velocity)
  30. {
  31. rb.isKinematic = false;
  32. rb.velocity = velocity;
  33. collected = false;
  34. }
  35. public async void BeCollect(int id)
  36. {
  37. rb.isKinematic = true;
  38. collected = true;
  39. tweenPos.from = transform;
  40. tweenPos.to = PlayersInput.instance[id].transform;
  41. tweenPos.duration = (tweenPos.from.position - tweenPos.to.position).magnitude / flySpeed;
  42. tweenPos.ResetToBeginning();
  43. tweenPos.PlayForward();
  44. await Task.Delay((int)(tweenPos.duration * 1000));
  45. BeGet(id);
  46. SoulInMap.souls.Remove(this);
  47. gameObject.SetActive(false);
  48. }
  49. public void BeGet(int id)
  50. {
  51. PlayersInput.instance[id].mp += addMp;
  52. }
  53. private void FixedUpdate()
  54. {
  55. if (isShoot)
  56. {
  57. time += Time.deltaTime;
  58. transform.position = Vector3.Lerp(from, to, time);
  59. if (Vector3.Distance(transform.position, to)<1f)
  60. {
  61. isShoot = false;
  62. }
  63. }
  64. if(transform.position.y>= altitude)
  65. {
  66. rb.velocity = Vector3.zero;
  67. }
  68. else
  69. {
  70. Vector3 velocity = rb.velocity;
  71. velocity.y = upSpeed;
  72. rb.velocity = velocity;
  73. }
  74. }
  75. public void BeShoot()
  76. {
  77. time = 0;
  78. isShoot = true;
  79. }
  80. }