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.

60 lines
1.4 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Thruster : MonoBehaviour {
  4. public float speed;
  5. public float thrustDuration;
  6. public bool shot = false;
  7. public bool attached = false;
  8. private ParticleSystem particles;
  9. [HideInInspector]
  10. public Rigidbody2D rigid;
  11. // Use this for initialization
  12. void Start () {
  13. rigid = GetComponent<Rigidbody2D>();
  14. particles = GetComponentInChildren<ParticleSystem>();
  15. particles.enableEmission = false;
  16. }
  17. // Update is called once per frame
  18. void FixedUpdate () {
  19. if (Input.anyKeyDown && attached) {
  20. StartCoroutine(fireEngines());
  21. }
  22. /*
  23. rigid.AddForceAtPosition(transform.right * speed, transform.position, ForceMode2D.Impulse);
  24. particles.enableEmission = true;
  25. shot = true;
  26. } else if (shot) {
  27. Destroy(gameObject);
  28. } else {
  29. particles.enableEmission = false;
  30. }
  31. */
  32. }
  33. private IEnumerator fireEngines() {
  34. Debug.Log("firing Engines");
  35. float startTime = Time.time;
  36. particles.enableEmission = true;
  37. while (Time.time - startTime < thrustDuration) {
  38. rigid.AddForceAtPosition(transform.right * speed, transform.position, ForceMode2D.Impulse);
  39. yield return new WaitForSeconds(0.01f);
  40. }
  41. Destroy(gameObject);
  42. yield return null;
  43. }
  44. }