From 5d0152f823713a8dc607c3afcd90c10148f90296 Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Sat, 20 Apr 2024 16:49:40 +0200 Subject: [PATCH] Support 360 aim --- Assets/Prefabs/Towers/Turret.prefab | 4 ++-- Assets/Scenes/TowerTest.unity | 5 +++++ Assets/Scripts/Utilities/EditableArc.cs | 25 ++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Assets/Prefabs/Towers/Turret.prefab b/Assets/Prefabs/Towers/Turret.prefab index 64543f4..1bdbae0 100644 --- a/Assets/Prefabs/Towers/Turret.prefab +++ b/Assets/Prefabs/Towers/Turret.prefab @@ -149,12 +149,12 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: orientation: 0 - samples: 15 + samples: 25 visualRadius: 1 knob: {fileID: 8756968480882234867} knobSensitiviy: 1 moveKnobAxisName: Mouse X - rotationMinMax: {x: -30, y: 30} + rotationMinMax: {x: 0, y: 360} --- !u!1 &2485007193584062128 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/TowerTest.unity b/Assets/Scenes/TowerTest.unity index 6da6690..79c6ee0 100644 --- a/Assets/Scenes/TowerTest.unity +++ b/Assets/Scenes/TowerTest.unity @@ -650,6 +650,11 @@ PrefabInstance: propertyPath: m_Name value: Turret objectReference: {fileID: 0} + - target: {fileID: 8981486734084153558, guid: 9415cb10a1bd579269301ca4f61a1554, + type: 3} + propertyPath: rotationMinMax.y + value: 360 + objectReference: {fileID: 0} - target: {fileID: 9116936080776508834, guid: 9415cb10a1bd579269301ca4f61a1554, type: 3} propertyPath: m_LocalPosition.x diff --git a/Assets/Scripts/Utilities/EditableArc.cs b/Assets/Scripts/Utilities/EditableArc.cs index 471ada0..8f7b042 100644 --- a/Assets/Scripts/Utilities/EditableArc.cs +++ b/Assets/Scripts/Utilities/EditableArc.cs @@ -37,7 +37,7 @@ public class EditableArc : MonoBehaviour Value.AddListener(UpdateKnobPosition); // Set default rotation to average between min max - Value.Value = (rotationMinMax.x + rotationMinMax.y) / 2f; + // Value.Value = (rotationMinMax.x + rotationMinMax.y) / 2f; Assert.IsNotNull(knob, $"No knob on {this}"); knob.OnDrag += PointerDraggedOnKnob; @@ -71,7 +71,7 @@ public class EditableArc : MonoBehaviour float sign = -1f; float delta = mouseMovement * knobSensitiviy * sign; - float newAngle = Mathf.Clamp(Value.Value + delta, rotationMinMax.x, rotationMinMax.y); + float newAngle = ClampAngle(Value.Value + delta, rotationMinMax.x, rotationMinMax.y); Value.Value = newAngle; } @@ -100,12 +100,31 @@ public class EditableArc : MonoBehaviour } lineRenderer.SetPositions(positions); + + // Set looop + Debug.Log(angle); + if (angle >= 360) + lineRenderer.loop = true; + else + lineRenderer.loop = false; } public Vector3 SamplePointOnArc(Vector3 startPoint, Vector3 endPoint, float radius, float t) { - float angle = Mathf.LerpAngle(rotationMinMax.x, rotationMinMax.y, t); + float angle = Mathf.Lerp(rotationMinMax.x, rotationMinMax.y, t); Vector3 dir = Quaternion.AngleAxis(angle, normal) * tangent; return transform.position + dir.normalized * radius; } + + public static float ClampAngle(float current, float min, float max) + { + float dtAngle = Mathf.Abs(((min - max) + 180) % 360 - 180); + float hdtAngle = dtAngle * 0.5f; + float midAngle = min + hdtAngle; + + float offset = Mathf.Abs(Mathf.DeltaAngle(current, midAngle)) - hdtAngle; + if (offset > 0) + current = Mathf.MoveTowardsAngle(current, midAngle, offset); + return current; + } }