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.

122 lines
3.2 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. startTime = Time.time;
  30. Collider2D thisCollider = GetComponent<Collider2D>();
  31. if (thisCollider != null) {
  32. foreach (Collider2D collider in ignoreList) {
  33. Physics2D.IgnoreCollision(thisCollider, collider);
  34. }
  35. }
  36. rigid = GetComponent<Rigidbody2D>();
  37. rigid.velocity = startVelocity * initVelocity;
  38. Destroy(gameObject, boostCoolDown * totalBoosts + 1);
  39. }
  40. // Update is called once per frame
  41. void FixedUpdate () {
  42. if (Time.time - startTime >= boostCoolDown && reCalibrate < totalBoosts) {
  43. if (reCalibrate == 0) {
  44. if (target != null) {
  45. targetPos = target.transform.position;
  46. }
  47. }
  48. if (target != null) {
  49. moveTorwards(target);
  50. }
  51. reCalibrate++;
  52. startTime = Time.time;
  53. }
  54. }
  55. private void moveTorwards(Vector3 pos) {
  56. if (boostEffect != null) {
  57. Debug.Log("Playing Boost");
  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. Instantiate(Explosion, transform.position,transform.rotation);
  87. Destroy(this.gameObject);
  88. }
  89. }