AimLoadChecker.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 Start()
  10. {
  11. }
  12. // Update is called once per frame
  13. void Update()
  14. {
  15. bool needActiveOutTip = false;
  16. if (ArmBow.ins && ArmBow.ins.IsCanShoot() && TargetBody.ins) {
  17. RaycastHit ray = CrossHair.ins.GetRaycastHit();
  18. if (ray.transform && ray.transform.gameObject.name == "TargetBody") {
  19. float mySpeed = Arrow.speed;
  20. if (GameAssistUI.ins) {
  21. mySpeed *= (1 + GameAssistUI.ins.shootScaleValue);
  22. }
  23. Vector3 destination = ray.point;
  24. Vector3 startPoint = BowCamera.ins.transform.position;
  25. float deltaX = Mathf.Sqrt(
  26. Mathf.Pow(destination.x - startPoint.x, 2) +
  27. Mathf.Pow(destination.z - startPoint.z,2)
  28. );
  29. float deltaY = destination.y - startPoint.y;
  30. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(mySpeed, 2);
  31. float b = deltaX;
  32. float c = a - deltaY;
  33. bool hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  34. if (hasParabolaAngle) {
  35. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  36. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  37. float parabolaAngle = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2)) / Mathf.PI * 180;
  38. needActiveOutTip = parabolaAngle > 5;
  39. }
  40. }
  41. }
  42. ActiveOutTip(needActiveOutTip);
  43. }
  44. public void ActiveOutTip(bool value)
  45. {
  46. if (outTip.activeSelf != value) {
  47. outTip.SetActive(value);
  48. }
  49. }
  50. }