You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.3 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent(typeof(Rigidbody2D))]
  4. public class MissileController : MonoBehaviour {
  5. public float speed = 1;
  6. public float initVelocity = 5;
  7. public float boostCoolDown = 0.3f;
  8. public float difficulty = 0.0f;
  9. public int totalBoosts = 2;
  10. public ParticleSystem boostEffect;
  11. public GameObject Explosion;
  12. [HideInInspector]
  13. public Vector3 startVelocity;
  14. [HideInInspector]
  15. public Vector3 targetPos;
  16. [HideInInspector]
  17. public GameObject target;
  18. [HideInInspector]
  19. public Rigidbody2D targetRigid;
  20. [HideInInspector]
  21. public Collider2D[] ignoreList;
  22. [HideInInspector]
  23. public InteractableObject spawningObject;
  24. private Rigidbody2D rigid;
  25. private float startTime = 0.0f;
  26. private int reCalibrate = 0;
  27. // Use this for initialization
  28. void Start () {
  29. DontDestroyOnLoad(gameObject);
  30. startTime = Time.time;
  31. Collider2D thisCollider = GetComponent<Collider2D>();
  32. if (thisCollider != null) {
  33. foreach (Collider2D collider in ignoreList) {
  34. Physics2D.IgnoreCollision(thisCollider, collider);
  35. }
  36. }
  37. rigid = GetComponent<Rigidbody2D>();
  38. rigid.velocity = startVelocity * initVelocity;
  39. Destroy(gameObject, boostCoolDown * totalBoosts + 1);
  40. }
  41. // Update is called once per frame
  42. void FixedUpdate () {
  43. if (Time.time - startTime >= boostCoolDown && reCalibrate < totalBoosts) {
  44. if (reCalibrate == 0) {
  45. if (target != null) {
  46. targetPos = target.transform.position;
  47. }
  48. }
  49. if (target != null) {
  50. moveTorwards(target);
  51. }
  52. reCalibrate++;
  53. startTime = Time.time;
  54. }
  55. }
  56. private void moveTorwards(Vector3 pos) {
  57. if (boostEffect != null) {
  58. boostEffect.startRotation = transform.rotation.eulerAngles.z * Mathf.Deg2Rad;
  59. boostEffect.Play();
  60. }
  61. Vector2 direction = (pos - transform.position).normalized;
  62. Debug.DrawLine(transform.position, pos, Color.green,0.3f);
  63. rigid.velocity = direction * speed;
  64. //rigid.AddForce(direction * speed, ForceMode2D.Impulse);
  65. }
  66. private void moveTorwards(GameObject target) {
  67. if (target == null)
  68. return;
  69. if (targetRigid != null && reCalibrate == 1) {
  70. Debug.DrawLine(targetPos, target.transform.position, Color.blue, 0.3f);
  71. Vector2 pos = Vector3.Lerp( targetPos,target.transform.position,difficulty);
  72. moveTorwards(pos);
  73. return;
  74. }
  75. moveTorwards(target.transform.position);
  76. }
  77. void OnCollisionEnter2D(Collision2D coll) {
  78. InteractableObject colScript = coll.gameObject.GetComponent<InteractableObject>();
  79. if (colScript == null || colScript == spawningObject ) {
  80. // Debug.Log("I ignored " + coll.gameObject.name);
  81. return;
  82. }
  83. //Debug.Log("I hit " + coll.gameObject.name);
  84. colScript.shot();
  85. if (Explosion != null) {
  86. GameObject explosion = (GameObject)Instantiate(Explosion, transform.position, transform.rotation);
  87. Destroy(explosion, 2.0f);
  88. }
  89. Destroy(this.gameObject);
  90. }
  91. }