GameDebug.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class GameDebug : MonoBehaviour
  6. {
  7. [SerializeField] Slider speedSlider;
  8. [SerializeField] Slider distanceSlider;
  9. [SerializeField] Text absoluteAngleText;
  10. [SerializeField] InputField offsetAngleInput;
  11. [SerializeField] Text logText;
  12. [SerializeField] GameObject outTip;
  13. bool loaded = false;
  14. public static GameDebug ins;
  15. void Awake()
  16. {
  17. ins = this;
  18. }
  19. void Start()
  20. {
  21. BowCamera.ins.enabled = false;
  22. onSlider();
  23. onSlider1();
  24. loaded = true;
  25. caluculateAbsoluteAngle();
  26. //增加射程卡
  27. PropInfo propInfo = new PropInfo();
  28. propInfo.id = 22;
  29. propInfo.count = 1;
  30. propInfo.inuse = true;
  31. LoginMgr.myUserInfo.bagList.Add(propInfo);
  32. }
  33. public void onSlider()
  34. {
  35. float value = speedSlider.value;
  36. speedSlider.GetComponentInChildren<Text>().text = "发射速度 " + (value).ToString("#0.00") + "m/s";
  37. Arrow.speed = GameMgr.RealSizeToGameSize(value);
  38. caluculateAbsoluteAngle();
  39. }
  40. public void onSlider1()
  41. {
  42. float value = distanceSlider.value;
  43. distanceSlider.GetComponentInChildren<Text>().text = "靶心距离 " + (value).ToString("#0.00") + "m";
  44. TargetBody target = GameObject.Find("GameArea/010").GetComponentInChildren<TargetBody>();
  45. target.SetDistance(value);
  46. caluculateAbsoluteAngle();
  47. }
  48. public void caluculateAbsoluteAngle() {
  49. if (!loaded) return;
  50. float mySpeed = Arrow.speed;
  51. if (GameAssistUI.ins) {
  52. mySpeed *= (1 + GameAssistUI.ins.shootScaleValue);
  53. }
  54. Vector3 destination = GameObject.Find("GameArea/010").transform.position;
  55. Vector3 startPoint = BowCamera.ins.transform.position;
  56. float deltaX = Vector2.Distance(
  57. new Vector2(destination.x, destination.z),
  58. new Vector2(startPoint.x, startPoint.z)
  59. );
  60. float deltaY = destination.y - startPoint.y;
  61. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(mySpeed, 2);
  62. float b = deltaX;
  63. float c = a - deltaY;
  64. bool hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  65. if (hasParabolaAngle) {
  66. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  67. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  68. float parabolaAngle = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2)) / Mathf.PI * 180;
  69. absoluteAngleText.text = "绝对角度: " + parabolaAngle + "°";
  70. outTip.SetActive(Mathf.Abs(parabolaAngle) > 5);
  71. } else {
  72. absoluteAngleText.text = "绝对角度: " + "无解";
  73. }
  74. }
  75. private void Log(string text) {
  76. logText.text = text;
  77. }
  78. public float GetOffsetAngle() {
  79. float angle = 0;
  80. try {
  81. angle = float.Parse(offsetAngleInput.text);
  82. } catch (System.Exception) {
  83. Log("偏移角度解析错误");
  84. }
  85. return angle;
  86. }
  87. public void ShowRes(Vector3 basePoint, Vector3 hitPoint, Vector3 offsetAngle) {
  88. basePoint /= 0.7f;
  89. hitPoint /= 0.7f;
  90. float dis = (hitPoint - basePoint).magnitude / 0.7f;
  91. string text =
  92. "准心瞄点 " + V3ToString(basePoint) + "\n" +
  93. "实际落点 " + V3ToString(hitPoint) + "\n" +
  94. "偏移角度 " + V3ToString(offsetAngle) + "\n" +
  95. "偏移距离 " + dis.ToString("#0.000") + "m";
  96. Log(text);
  97. }
  98. public string V3ToString(Vector3 v) {
  99. return "(" + v.x.ToString("#0.00") + ", " + v.y.ToString("#0.00") + ", " + v.z.ToString("#0.00") + ")";
  100. }
  101. public void deleteAllArrows() {
  102. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  103. foreach (var arrow in arrows)
  104. {
  105. if (!arrow.hasDoneNextShoot) continue;
  106. try {
  107. GameObject.Destroy(arrow.gameObject);
  108. } catch (UnityException e) {
  109. Debug.Log("Delete Arrow Error\n" + e.Message);
  110. }
  111. }
  112. }
  113. }