BezierFire.cs 895 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BezierFire : MonoBehaviour
  5. {
  6. public Transform target;
  7. public float r =10;
  8. public Bullet bulletPrefab;
  9. private void Start()
  10. {
  11. StartFire();
  12. }
  13. public void StartFire()
  14. {
  15. StartCoroutine(Fire());
  16. }
  17. public void StopFire()
  18. {
  19. StopAllCoroutines();
  20. }
  21. public Vector3 GetRandomPoint(float r)
  22. {
  23. return transform.position + new Vector3(Random.Range(-r, r), Random.Range(-r, r), Random.Range(-r, r));
  24. }
  25. IEnumerator Fire()
  26. {
  27. while (true)
  28. {
  29. Bullet bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
  30. StartCoroutine(bullet.Move(bullet.transform.position, GetRandomPoint(r), target));
  31. yield return new WaitForSeconds(0.01f);
  32. }
  33. }
  34. }