AimLoadChecker.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /**瞄准器负载检测 */
  6. public class AimLoadChecker : MonoBehaviour
  7. {
  8. [SerializeField] GameObject outTip;
  9. void Update()
  10. {
  11. bool needActiveOutTip = false;
  12. if (ArmBow.ins && ArmBow.ins.IsCanShoot() && TargetBody.ins) {
  13. RaycastHit ray = CrossHair.ins.GetRaycastHit();
  14. if (ray.transform && ray.transform.gameObject.name == "TargetBody") {
  15. float mySpeed = Arrow.speed;
  16. if (GameAssistUI.ins) {
  17. mySpeed *= GameAssistUI.ins.shootScaleValue;
  18. }
  19. Vector3 destination = ray.point;
  20. Vector3 startPoint = BowCamera.ins.transform.position;
  21. float deltaX = Mathf.Sqrt(
  22. Mathf.Pow(destination.x - startPoint.x, 2) +
  23. Mathf.Pow(destination.z - startPoint.z,2)
  24. );
  25. float deltaY = destination.y - startPoint.y;
  26. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(mySpeed, 2);
  27. float b = deltaX;
  28. float c = a - deltaY;
  29. bool hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  30. if (hasParabolaAngle) {
  31. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  32. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  33. float parabolaAngle = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2)) / Mathf.PI * 180;
  34. needActiveOutTip = parabolaAngle > 5;
  35. }
  36. }
  37. }
  38. ActiveOutTip(needActiveOutTip);
  39. }
  40. public void ActiveOutTip(bool value)
  41. {
  42. if (outTip.activeSelf != value) {
  43. outTip.SetActive(value);
  44. }
  45. }
  46. }