MultiShot.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MultiShot : MonoBehaviour
  5. {
  6. [Header("主银")]
  7. public Character owner;
  8. [Header("弓箭预制体")]
  9. public GameObject arrow;
  10. [Header("齐发参数")]
  11. public int num;
  12. public float angle;
  13. public float arrivalAngle;
  14. public int damage;
  15. public bool tryy;
  16. public float gravity = 9.8f; // 重力加速度
  17. public float speed;
  18. public void Shoot()
  19. {
  20. float angleInterval = num > 1 ? angle / (float)(num - 1) : 0f;
  21. for (int i = 0; i < num; i++)
  22. {
  23. float ang = 0;
  24. GameObject obj = Instantiate(arrow);
  25. MultiArrowController arrowController = obj.AddComponent<MultiArrowController>();
  26. Vector3 dir = Vector3.zero;
  27. if (owner.bodyTrans.localScale.x > 0)
  28. {
  29. ang = 180 - arrivalAngle - angle / 2 + i * angleInterval;
  30. }
  31. else
  32. {
  33. ang = arrivalAngle - angle / 2 + i * angleInterval;
  34. }
  35. ang = ang / 180 * Mathf.PI;
  36. dir = new Vector3(Mathf.Cos(ang), Mathf.Sin(ang), 0);
  37. obj.GetComponent<Bullet>().BeShoot(owner, owner.transform.position + Vector3.up, dir, damage, 0, false, 0);
  38. arrowController.Initialize(gravity, speed);
  39. }
  40. }
  41. private void Update()
  42. {
  43. if (tryy)
  44. {
  45. tryy = false;
  46. Shoot();
  47. }
  48. }
  49. }