initial commit
This commit is contained in:
79
Assets/Scripts/Lesson_1/DrawDistanceLine.cs
Normal file
79
Assets/Scripts/Lesson_1/DrawDistanceLine.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(LineRenderer))]
|
||||
public class DrawDistanceLine : MonoBehaviour
|
||||
{
|
||||
private float radiusX;
|
||||
private float radiusZ;
|
||||
|
||||
private float startAngle;
|
||||
private float endAngle;
|
||||
|
||||
[Header("ßê³ñòü äóãè")]
|
||||
public int segments = 50;
|
||||
|
||||
[Header("Öåíòð äóãè")]
|
||||
private Vector3 center;
|
||||
|
||||
[Header("²íôîðìàö³ÿ (ò³ëüêè äëÿ ÷èòàííÿ)")]
|
||||
[SerializeField] private float arcLength = 0f;
|
||||
|
||||
private LineRenderer lineRenderer;
|
||||
|
||||
void Start()
|
||||
{
|
||||
radiusX = GetComponent<RunlinePlacer>().radiusX;
|
||||
radiusZ = GetComponent<RunlinePlacer>().radiusZ;
|
||||
center = GetComponent<RunlinePlacer>().center;
|
||||
|
||||
lineRenderer = GetComponent<LineRenderer>();
|
||||
|
||||
lineRenderer.startColor = new Color(1f, 1f, 1f);
|
||||
lineRenderer.endColor = new Color(1f, 1f, 1f);
|
||||
lineRenderer.startWidth = 1f;
|
||||
lineRenderer.endWidth = 1f;
|
||||
lineRenderer.useWorldSpace = true;
|
||||
lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
|
||||
}
|
||||
|
||||
public void DrawDistanceArc()
|
||||
{
|
||||
|
||||
startAngle = GetComponent<RunlinePlacer>().GetStartLineAngle();
|
||||
endAngle = GetComponent<RunlinePlacer>().GetEndLineAngle();
|
||||
|
||||
if (startAngle > endAngle) endAngle += 2 * Mathf.PI;
|
||||
|
||||
lineRenderer.positionCount = segments + 1;
|
||||
float angleStep = (endAngle - startAngle) / segments;
|
||||
|
||||
Vector3 prevPos = Vector3.zero;
|
||||
arcLength = 0f;
|
||||
|
||||
for (int i = 0; i <= segments; i++)
|
||||
{
|
||||
float angle = startAngle + i * angleStep;
|
||||
float x = Mathf.Cos(angle) * radiusX;
|
||||
float z = Mathf.Sin(angle) * radiusZ;
|
||||
lineRenderer.SetPosition(i, new Vector3(x, 0f, z));
|
||||
Vector3 pos = new Vector3(x, 0f, z) + center;
|
||||
|
||||
if (i > 0)
|
||||
arcLength += Vector3.Distance(pos, prevPos);
|
||||
|
||||
prevPos = pos;
|
||||
}
|
||||
|
||||
GameObject startLine = GetComponent<RunlinePlacer>().GetStartLineGameObject();
|
||||
startLine.GetComponent<LineData>().SetDistanceText((int)arcLength);
|
||||
ClearArc();
|
||||
}
|
||||
public void ClearArc()
|
||||
{
|
||||
if (lineRenderer != null)
|
||||
{
|
||||
lineRenderer.positionCount = 0;
|
||||
arcLength = 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user