Missile.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. [RequireComponent(typeof(AudioSource))]
  5. public class Missile : MonoBehaviour
  6. {
  7. #region Fields
  8. [HideInInspector]
  9. public Vector3 directionFrom = Vector3.zero;
  10. [HideInInspector]
  11. private int hitCount = 0;
  12. private float damage;
  13. private float maxHits;
  14. private float impactForce;
  15. private float maxInaccuracy;
  16. private float speed;
  17. private float lifetime = 5f;
  18. [HideInInspector]
  19. public string gunName = "";
  20. private Vector3 velocity = Vector3.zero;
  21. private Vector3 newPos = Vector3.zero;
  22. private Vector3 oldPos = Vector3.zero;
  23. private bool hasHit = false;
  24. private Vector3 direction;
  25. [Space(5)]
  26. public AudioSource AudioReferences;
  27. [SerializeField]private AudioClip concreteSound;
  28. [SerializeField]private AudioClip genericSound;
  29. public Vector2 mPicht = new Vector2(1.0f, 1.5f);
  30. [HideInInspector]
  31. public enum HitType
  32. {
  33. CONCRETE,
  34. GENERIC
  35. };
  36. public GameObject missileHitObject;
  37. #endregion
  38. public void SetUp (MissileInfo info) // information sent from gun to bullet to change bullet properties
  39. {
  40. damage = info.damage;
  41. impactForce = info.impactForce;
  42. maxHits = info.maxPenetration; // max number of bullet impacts before bullet is destroyed
  43. maxInaccuracy = info.maxspread;
  44. speed = info.speed;
  45. directionFrom = info.position;
  46. direction = transform.TransformDirection (1f, Random.Range(-maxInaccuracy, maxInaccuracy), Random.Range(-maxInaccuracy, maxInaccuracy));
  47. newPos = transform.position;
  48. oldPos = newPos;
  49. velocity = speed * transform.right;
  50. Destroy (gameObject, lifetime);
  51. }
  52. Vector3 dir = Vector3.zero;
  53. void Update()
  54. {
  55. if (hasHit)
  56. return;
  57. // assume we move all the way
  58. newPos += (velocity + direction) * Time.deltaTime;
  59. // Check if we hit anything on the way
  60. dir = newPos - oldPos;
  61. float dist = dir.magnitude;
  62. if (dist > 0)
  63. {
  64. // normalize
  65. dir /= dist;
  66. RaycastHit[] hits = Physics.RaycastAll (oldPos, dir, dist);
  67. Debug.DrawLine (oldPos, newPos, Color.red);
  68. for (int i = 0; i < hits.Length; i++)
  69. {
  70. RaycastHit hit = hits[i];
  71. newPos = hit.point;
  72. OnHit(hit);
  73. if (hitCount >= maxHits)
  74. {
  75. hasHit = true;
  76. Destroy(gameObject);
  77. }
  78. }
  79. }
  80. oldPos = transform.position;
  81. transform.position = newPos;
  82. }
  83. void OnHit(RaycastHit hit)
  84. {
  85. Ray mRay = new Ray (transform.position, transform.forward);
  86. if (hit.rigidbody != null && !hit.rigidbody.isKinematic) // if we hit a rigi body... apply a force
  87. {
  88. float mAdjust = 1.0f / (Time.timeScale * (0.02f / Time.fixedDeltaTime));
  89. hit.rigidbody.AddForceAtPosition(((mRay.direction * impactForce) / Time.timeScale) / mAdjust, hit.point);
  90. }
  91. switch (hit.transform.tag) // decide what the bullet collided with and what to do with it
  92. {
  93. case "Concrete":
  94. hitCount += 2; // add 2 hits to counter... concrete is hard
  95. // type = HitType.CONCRETE;
  96. if (hitCount >= maxHits)
  97. StickToTheWall (hit, hit.transform.gameObject);
  98. break;
  99. case "Generic" :
  100. hitCount++;
  101. // type = HitType.GENERIC;
  102. if (hitCount >= maxHits)
  103. StickToTheWall (hit, hit.transform.gameObject);
  104. break;
  105. case "Invisible": //do nothing
  106. case "Projectile": // do nothing if 2 bullets collide
  107. default:
  108. break;
  109. }
  110. }
  111. void StickToTheWall (RaycastHit hit, GameObject go)
  112. {
  113. Vector3 contact = hit.point;
  114. GameObject newMissiletHit = Instantiate (missileHitObject, contact, this.transform.rotation) as GameObject;
  115. // newMissiletHit.transform.parent = go.transform;
  116. }
  117. }