Bullet.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Bullet : MonoBehaviour
  5. {
  6. public float speed = 5;
  7. public void Start()
  8. {
  9. Destroy(gameObject, 15);
  10. }
  11. private void OnDestroy()
  12. {
  13. StopAllCoroutines();
  14. }
  15. public IEnumerator Move(Vector3 start, Vector3 midPoint, Transform target)
  16. {
  17. for (float i= 0; i <= 1; i += Time.deltaTime )
  18. {
  19. Vector3 p1 = Vector3.Lerp(start, midPoint, i);
  20. Vector3 p2 = Vector3.Lerp(midPoint, target.position, i);
  21. Vector3 p = Vector3.Lerp(p1, p2, i);
  22. //让子弹移动到p点
  23. yield return StartCoroutine(MoveToPoint(p));
  24. }
  25. yield return StartCoroutine(MoveToObject(target));
  26. }
  27. IEnumerator MoveToPoint(Vector3 p)
  28. {
  29. yield return null;
  30. while (Vector3.Distance(transform.position, p) > 0.1f)
  31. {
  32. Vector3 dir = p - transform.position;
  33. transform.up =dir;
  34. transform.position = Vector3.MoveTowards(transform.position, p, speed * Time.deltaTime);
  35. yield return null;
  36. }
  37. }
  38. IEnumerator MoveToObject(Transform target)
  39. {
  40. yield return null;
  41. while (Vector3.Distance(transform.position, target.position) > 0.1f)
  42. {
  43. Vector3 dir = target.position - transform.position;
  44. transform.up =dir;
  45. transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
  46. yield return null;
  47. }
  48. }
  49. private void OnCollisionEnter(Collision collision)
  50. {
  51. if (collision.gameObject.name == "Target")
  52. {
  53. //伤害结算
  54. Destroy(gameObject);
  55. }
  56. }
  57. }