ArrowBehavior.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ArrowBehavior : MonoBehaviour
  5. {
  6. private static GamingManager _gamingManager;
  7. private bool bUsePhysics;
  8. private Vector3 Direction_Kinematic;
  9. private float Speed_Kinematic;
  10. private float Lifetime = 0.0f;
  11. private const float LifetimeLimit = 15f;
  12. private const float MissShootTime = 3f;
  13. private bool bHit = false;
  14. public delegate void MissShoot();
  15. public event MissShoot OnMissShoot;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. _gamingManager = GameObject.Find("GameManager").GetComponent<GamingManager>();
  20. }
  21. public void Init(float speed, Vector3 dir, bool use_physics)
  22. {
  23. bUsePhysics = use_physics;
  24. Rigidbody rb = gameObject.GetComponent<Rigidbody>();
  25. if (bUsePhysics)
  26. {
  27. rb.useGravity = true;
  28. rb.isKinematic = false;
  29. rb.velocity = dir * speed;
  30. }
  31. else
  32. {
  33. rb.useGravity = false;
  34. rb.isKinematic = true;
  35. rb.velocity = Vector3.zero;
  36. Speed_Kinematic = speed;
  37. Direction_Kinematic = dir;
  38. }
  39. }
  40. // Update is called once per frame
  41. void Update()
  42. {
  43. if (!_gamingManager.IsGamePaused())
  44. {
  45. if (!bUsePhysics)
  46. {
  47. gameObject.transform.position = gameObject.transform.position + Direction_Kinematic * Time.deltaTime * Speed_Kinematic;
  48. }
  49. Lifetime += Time.deltaTime;
  50. if (Lifetime >= MissShootTime)
  51. {
  52. if (!bHit)
  53. {
  54. OnMissShoot.Invoke();
  55. }
  56. }
  57. if (Lifetime > LifetimeLimit)
  58. {
  59. Destroy(gameObject);
  60. }
  61. }
  62. }
  63. public void HitAFruit()
  64. {
  65. bHit = true;
  66. }
  67. void OnTriggerEnter(Collider other)
  68. {
  69. if (other.gameObject.tag == "StaticCollisionScene")
  70. {
  71. Rigidbody rb = gameObject.GetComponent<Rigidbody>();
  72. rb.isKinematic = true;
  73. rb.velocity = Vector3.zero;
  74. }
  75. }
  76. }