Projectile.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Projectile : MonoBehaviour {
  5. public int power = 10; //弾のパワー
  6. public float range = 10;//移動範囲
  7. public EnemyMovement target; //敵のscript
  8. public float travelSpeed; //弾のスピード
  9. public bool homing = false;
  10. public float slowTime = 0;
  11. [Range(0.0f, 1.0f)]public float homingRate = 0.1f;
  12. Vector3 targetDirection = Vector3.up;
  13. public GameObject impactPrefab;
  14. public GameObject ExplosionBoom;
  15. Vector3 startPosition;
  16. Vector3 targetLastPosition = Vector3.zero;
  17. const float lockonThreshold = 1.0f;
  18. bool firstFrame = false; //prevents unwanted retargeting on the first frame.
  19. // Use this for initialization
  20. void Start () {
  21. if(!homing) targetDirection = target.transform.position-transform.position;
  22. startPosition = transform.position;
  23. targetLastPosition = target.transform.position;
  24. }
  25. // Update is called once per frame
  26. void FixedUpdate () {
  27. if (homing)
  28. {
  29. if (target)
  30. {
  31. if (Vector3.Distance(targetLastPosition, target.transform.position) > lockonThreshold && !firstFrame)
  32. {
  33. Retarget();
  34. //Debug.Log("Retargeting...");
  35. }
  36. targetDirection = Vector3.Slerp(
  37. targetDirection,
  38. target.transform.position - transform.position,
  39. homingRate);
  40. targetLastPosition = target.transform.position;
  41. firstFrame = false;
  42. }
  43. else
  44. {
  45. Retarget();
  46. }
  47. targetDirection.Normalize();
  48. transform.Translate(targetDirection * Time.deltaTime * travelSpeed);
  49. }
  50. if (!homing) {
  51. homingRate += Time.deltaTime * travelSpeed;
  52. // transform.position = Vector3.Slerp(startPosition, targetLastPosition, homingRate);
  53. targetDirection = Vector3.Slerp(
  54. Vector3.zero,
  55. targetDirection,
  56. homingRate);
  57. targetDirection.Normalize();
  58. transform.Translate(targetDirection * Time.deltaTime * travelSpeed);
  59. if (Vector3.Distance(transform.position, targetLastPosition) < 1) Destroy(gameObject);
  60. }
  61. if (Vector3.Distance(transform.position, startPosition) > range) Destroy(gameObject);
  62. }
  63. private void OnTriggerEnter(Collider other)
  64. {
  65. //if (other.tag == "floor") Destroy(gameObject);
  66. //Debug.Log("Hit something.");
  67. EnemyMovement hitEnemy = other.GetComponent<EnemyMovement>();
  68. if (hitEnemy != null)
  69. {
  70. hitEnemy.Damage(power);
  71. if(slowTime > 0)
  72. {
  73. hitEnemy.SlowDown(slowTime);
  74. }
  75. //Debug.Log("Enemy hit!!");
  76. Destroy(gameObject);
  77. }
  78. }
  79. void Retarget()
  80. {
  81. Collider[] otherEnemies = Physics.OverlapSphere(transform.position, range);
  82. foreach (var col in otherEnemies)
  83. {
  84. EnemyMovement other = col.GetComponent<EnemyMovement>();
  85. if (other != null)
  86. {
  87. target = other;
  88. break;
  89. }
  90. }
  91. }
  92. private void OnDestroy()
  93. {
  94. if(impactPrefab)
  95. {
  96. Instantiate(impactPrefab, transform.position, Quaternion.identity);
  97. }
  98. if (ExplosionBoom)
  99. {
  100. GameObject boom = ExplosionBoom;
  101. boom.GetComponent<GunExplosionBoom>().damage = power;
  102. Instantiate(boom, transform.position, Quaternion.identity);
  103. }
  104. }
  105. }