DrawParabola.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Core.Utility;
  2. using UnityEngine;
  3. public class DrawParabola : MonoBehaviour
  4. {
  5. public Vector3 startPos = new Vector3(0, 0, 0);
  6. public Vector3 targetPos = new Vector3(10, 5, 10);
  7. public int segments = 100; // 抛物线段数
  8. private LineRenderer lineRenderer;
  9. private Parabola3DPath path;
  10. [ContextMenu("dasdasd")]
  11. public void Start22()
  12. {
  13. // 初始化 LineRenderer
  14. lineRenderer = gameObject.AddComponent<LineRenderer>();
  15. lineRenderer.positionCount = segments + 1;
  16. lineRenderer.useWorldSpace = true;
  17. lineRenderer.startWidth = 0.1f;
  18. lineRenderer.endWidth = 0.1f;
  19. // 初始化 Parabola3DPath
  20. path = new Parabola3DPath(startPos, targetPos);
  21. // 绘制抛物线
  22. DrawPath();
  23. }
  24. void DrawPath()
  25. {
  26. for (int i = 0; i <= segments; i++)
  27. {
  28. float t = i * (path.totalFlightTime / segments);
  29. Vector3 position = path.GetPositionAtTime(t);
  30. lineRenderer.SetPosition(i, position);
  31. }
  32. }
  33. }