using UnityEngine; public enum ArcOrientation { HORIZONTAL, VERTICAL, } [RequireComponent(typeof(LineRenderer))] public class EditableArc : MonoBehaviour { [SerializeField] private ArcOrientation orientation = ArcOrientation.HORIZONTAL; [SerializeField, Range(5, 50)] private int samples = 15; [SerializeField] private float visualRadius = 1f; [SerializeField] private Vector2 rotationMinMax = new Vector2(-30f, 30f); private LineRenderer lineRenderer; public Observer Value { get; private set; } = new(0); public Vector2 RotationMinMax => rotationMinMax; private void Awake() { lineRenderer = GetComponent(); Value.AddListener(UpdateArc); Value.Value = (rotationMinMax.x + rotationMinMax.y) / 2f; } private void Update() { UpdateArc(Value.Value); } private void UpdateArc(float rotation) { Vector3 normal; Vector3 tangent; if (orientation == ArcOrientation.HORIZONTAL) { normal = transform.up; tangent = transform.right; } else if (orientation == ArcOrientation.VERTICAL) { normal = transform.right; tangent = transform.up; } else { throw new System.Exception("WTFFFFF HAVE YOU DONE YOU MF!!?"); } float angle = rotationMinMax.y - rotationMinMax.x; float v = (Mathf.PI - Value * Mathf.Deg2Rad) / 2f; Vector3 start = Quaternion.AngleAxis(rotationMinMax.x, normal) * tangent; Vector3 end = Quaternion.AngleAxis(rotationMinMax.y, normal) * tangent; // Sample LineRenderer points lineRenderer.positionCount = samples + 1; Vector3[] positions = new Vector3[samples + 1]; float stepSize = 1f / samples; float t = 0; for (int i = 0; i <= samples; i++) { positions[i] = SamplePointOnArc(start, end, visualRadius, t); // Debug.Log($"t: = {t}, pos: {positions[i]}"); t += stepSize; } lineRenderer.SetPositions(positions); } public Vector3 SamplePointOnArc(Vector3 startPoint, Vector3 endPoint, float radius, float t) { // Calculate the angle between start and end points float angle = Vector3.Angle(startPoint, endPoint) * t; // Calculate the perpendicular vector from start to end Vector3 axis = Vector3.Cross(startPoint, endPoint).normalized; // Calculate the position of the point on the arc Vector3 pointOnArc = Quaternion.AngleAxis(angle, axis) * startPoint; // Scale the point by the radius pointOnArc *= radius; return pointOnArc; } }