diff --git a/Assets/Scenes/AlgoDev.unity b/Assets/Scenes/AlgoDev.unity index 074f624..6fbf35c 100644 --- a/Assets/Scenes/AlgoDev.unity +++ b/Assets/Scenes/AlgoDev.unity @@ -355,6 +355,10 @@ MonoBehaviour: - 41.505398 - 42.154396 - 42.503956 + line: {fileID: 1026138248} + offsetMultipliers: {x: 1, y: 1} + objectToMove: {fileID: 1120561245} + MoveCmd: 0 --- !u!4 &335227241 Transform: m_ObjectHideFlags: 0 @@ -431,7 +435,70 @@ LineRenderer: m_SortingOrder: 0 m_Positions: - {x: 0, y: 0, z: 0} - - {x: 1, y: 0, z: 0} + - {x: 1.007976, y: -1.007976, z: 0} + - {x: 2.9457881, y: 0.92983603, z: 0} + - {x: 3.2396343, y: 0.6359899, z: 0} + - {x: 3.535689, y: 0.93204474, z: 0} + - {x: 3.7529073, y: 0.7148266, z: 0} + - {x: 4.945534, y: 1.9074535, z: 0} + - {x: 5.2670794, y: 1.5859084, z: 0} + - {x: 5.5404615, y: 1.8592906, z: 0} + - {x: 5.838297, y: 1.5614552, z: 0} + - {x: 8.023146, y: 3.746304, z: 0} + - {x: 9.277683, y: 2.4917665, z: 0} + - {x: 9.612368, y: 2.8264508, z: 0} + - {x: 9.978131, y: 2.4606872, z: 0} + - {x: 11.05203, y: 3.5345855, z: 0} + - {x: 11.563883, y: 3.0227323, z: 0} + - {x: 11.862214, y: 3.3210635, z: 0} + - {x: 12.16262, y: 3.020658, z: 0} + - {x: 13.06958, y: 3.9276185, z: 0} + - {x: 13.596535, y: 3.4006639, z: 0} + - {x: 13.906741, y: 3.7108703, z: 0} + - {x: 16.131954, y: 1.4856572, z: 0} + - {x: 17.358862, y: 2.712565, z: 0} + - {x: 17.68974, y: 2.3816876, z: 0} + - {x: 17.955818, y: 2.6477666, z: 0} + - {x: 18.111351, y: 2.4922338, z: 0} + - {x: 18.661879, y: 3.0427613, z: 0} + - {x: 19.170216, y: 2.5344243, z: 0} + - {x: 19.696922, y: 3.061131, z: 0} + - {x: 20.262955, y: 2.4950986, z: 0} + - {x: 20.802982, y: 3.0351262, z: 0} + - {x: 21.02081, y: 2.8172994, z: 0} + - {x: 21.289953, y: 3.0864434, z: 0} + - {x: 21.832458, y: 2.5439382, z: 0} + - {x: 22.291527, y: 3.0030065, z: 0} + - {x: 23.767723, y: 1.5268102, z: 0} + - {x: 24.27649, y: 2.0355763, z: 0} + - {x: 25.550953, y: 0.7611127, z: 0} + - {x: 25.946125, y: 1.1562848, z: 0} + - {x: 26.332308, y: 0.770102, z: 0} + - {x: 26.837963, y: 1.2757573, z: 0} + - {x: 27.237825, y: 0.875895, z: 0} + - {x: 27.88322, y: 1.5212903, z: 0} + - {x: 28.390924, y: 1.0135865, z: 0} + - {x: 29.355066, y: 1.9777284, z: 0} + - {x: 29.900814, y: 1.4319806, z: 0} + - {x: 30.385643, y: 1.9168096, z: 0} + - {x: 32.436077, y: -0.13362455, z: 0} + - {x: 33.891426, y: 1.3217244, z: 0} + - {x: 34.48847, y: 0.72468233, z: 0} + - {x: 34.974228, y: 1.2104421, z: 0} + - {x: 35.49399, y: 0.6906781, z: 0} + - {x: 36.081886, y: 1.2785726, z: 0} + - {x: 36.373074, y: 0.9873853, z: 0} + - {x: 36.975796, y: 1.5901074, z: 0} + - {x: 37.52076, y: 1.0451436, z: 0} + - {x: 37.923553, y: 1.4479375, z: 0} + - {x: 38.40126, y: 0.9702306, z: 0} + - {x: 39.078022, y: 1.6469922, z: 0} + - {x: 39.528507, y: 1.196507, z: 0} + - {x: 40.53657, y: 2.2045712, z: 0} + - {x: 41.026608, y: 1.7145352, z: 0} + - {x: 41.505398, y: 2.1933255, z: 0} + - {x: 42.154396, y: 1.5443273, z: 0} + - {x: 42.503956, y: 1.893887, z: 0} m_Parameters: serializedVersion: 3 widthMultiplier: 0.1 @@ -506,6 +573,90 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1120561245 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1120561247} + - component: {fileID: 1120561246} + m_Layer: 0 + m_Name: Square + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!212 &1120561246 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1120561245} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 7482667652216324306, guid: 311925a002f4447b3a28927169b83ea6, type: 3} + m_Color: {r: 0.21888566, g: 1, b: 0, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!4 &1120561247 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1120561245} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 0.5, y: 0.5, z: 0.5} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 @@ -513,3 +664,4 @@ SceneRoots: - {fileID: 231266800} - {fileID: 1026138249} - {fileID: 335227241} + - {fileID: 1120561247} diff --git a/Assets/Scripts/ProtoTyping/AlgorithmTest.cs b/Assets/Scripts/ProtoTyping/AlgorithmTest.cs index bd5895f..a321c09 100644 --- a/Assets/Scripts/ProtoTyping/AlgorithmTest.cs +++ b/Assets/Scripts/ProtoTyping/AlgorithmTest.cs @@ -7,15 +7,29 @@ public class AlgorithmTest : MonoBehaviour public List allHits = new List(); public LineRenderer line; public Vector2 offsetMultipliers = Vector2.one; - + public GameObject objectToMove; // The GameObject to move + + private Vector3[] points; + + + public bool MoveCmd; + + + Coroutine movingCoroutine; void OnDrawGizmos(){ RegenerateLines(); + + if(MoveCmd){ + MoveCmd = false; + if(movingCoroutine!= null){StopCoroutine(movingCoroutine);} + movingCoroutine = StartCoroutine(MoveAlongPath()); + } } void RegenerateLines(){ - Vector3[] points = new Vector3[allHits.Count]; + points = new Vector3[allHits.Count]; - for(int i=0; i < points.Length; i++){ + for (int i = 0; i < points.Length; i++) { points[i] = GetPosition(i); } @@ -32,7 +46,7 @@ public class AlgorithmTest : MonoBehaviour // Calculate diff and prevY if the index is greater than 0 if (index > 0) { diff = allHits[index] - allHits[index - 1]; - prevY = GetPosition(index - 1).y; // Recursively get the previous Y value + prevY = points[index - 1].y; // Use the already calculated point to avoid recursion } float flipSide = index % 2 == 0 ? 1 : -1; @@ -41,4 +55,68 @@ public class AlgorithmTest : MonoBehaviour return new Vector3(hitX * offsetMultipliers.x, y, 0); } + // Call this function to start moving the GameObject along the path + public void StartMovement() { + if (objectToMove != null && points != null && points.Length > 1) { + StartCoroutine(MoveAlongPath()); + } + } + + private IEnumerator MoveAlongPath() { + float timer=0; + while(timer < allHits[allHits.Count-1]){ + timer +=Time.deltaTime; + yield return null; + objectToMove.transform.position = GetPositionAtTime(timer); + } + // for (int i = 0; i < allHits.Count - 1; i++) { + // Vector3 startPoint = points[i]; + // Vector3 endPoint = points[i + 1]; + // float duration = allHits[i + 1] - allHits[i]; + + // yield return StartCoroutine(MoveFromTo(objectToMove, startPoint, endPoint, duration)); + // } + } + + private IEnumerator MoveFromTo(GameObject obj, Vector3 start, Vector3 end, float duration) { + float elapsedTime = 0; + + while (elapsedTime < duration) { + obj.transform.position = Vector3.Lerp(start, end, elapsedTime / duration); + elapsedTime += Time.deltaTime; + yield return null; + } + + obj.transform.position = end; + } + public Vector3 GetPositionAtTime(float time) { + if (points == null || points.Length < 2) { + return Vector3.zero; // Return zero if points are not set up + } + + // If the time is before the first hit, return the first position + if (time <= allHits[0]) { + return points[0]; + } + + // If the time is after the last hit, return the last position + if (time >= allHits[allHits.Count - 1]) { + return points[points.Length - 1]; + } + + // Find the current segment based on the given time + for (int i = 0; i < allHits.Count - 1; i++) { + if (time >= allHits[i] && time < allHits[i + 1]) { + Vector3 startPoint = points[i]; + Vector3 endPoint = points[i + 1]; + float segmentDuration = allHits[i + 1] - allHits[i]; + float t = (time - allHits[i]) / segmentDuration; + + return Vector3.Lerp(startPoint, endPoint, t); + } + } + + return Vector3.zero; // Return zero if no valid segment is found (should not happen) +} + }