diff --git a/Assets/Scripts/FixSystemNew/BodyModule/GearFollower.cs b/Assets/Scripts/FixSystemNew/BodyModule/GearFollower.cs index f6b4a2631..9395e45bd 100644 --- a/Assets/Scripts/FixSystemNew/BodyModule/GearFollower.cs +++ b/Assets/Scripts/FixSystemNew/BodyModule/GearFollower.cs @@ -215,45 +215,47 @@ public class GearFollowerWithVisualCut : MonoBehaviour } } - private void MoveAlongEdge(Vector2 mousePos) + private void MoveAlongEdge(Vector2 mousePos) +{ + // 当前段的两个点 + Vector2 a = edgePoints[currentEdgeIndex]; + Vector2 b = edgePoints[currentEdgeIndex + 1]; + + // 投影鼠标位置到当前段 + Vector2 projected = ProjectPointOnSegment(mousePos, a, b); + + // 鼠标离齿轮足够远才移动(模拟牵引力) + float tractionThreshold = 0.1f; + float distToMouse = Vector2.Distance(transform.position, mousePos); + if (distToMouse < tractionThreshold) + return; + + // 移动齿轮朝向投影点 + transform.position = Vector2.MoveTowards(transform.position, projected, tractionSpeed * Time.deltaTime); + + // 标记切割 + if (!cutSegments[currentEdgeIndex]) { - float closestDist = float.MaxValue; - Vector2 closestPoint = Vector2.zero; - int closestSegmentStartIndex = 0; - - for (int i = 0; i < edgePoints.Count - 1; i++) - { - Vector2 a = edgePoints[i]; - Vector2 b = edgePoints[i + 1]; - - Vector2 projected = ProjectPointOnSegment(mousePos, a, b); - float dist = Vector2.Distance(mousePos, projected); - - if (dist < closestDist) - { - closestDist = dist; - closestPoint = projected; - closestSegmentStartIndex = i; - } - } - - float stopThreshold = 0.05f; - if (Vector2.Distance(transform.position, closestPoint) < stopThreshold) - { - return; - } - - transform.position = Vector2.MoveTowards(transform.position, closestPoint, tractionSpeed * Time.deltaTime); - currentEdgeIndex = closestSegmentStartIndex; - - // 标记当前线段已被切割 - if (!cutSegments[currentEdgeIndex]) - { - cutSegments[currentEdgeIndex] = true; - Debug.Log($"切割了线段 {currentEdgeIndex} ({edgePoints[currentEdgeIndex]}->{edgePoints[currentEdgeIndex + 1]})"); - } + cutSegments[currentEdgeIndex] = true; + Debug.Log($"切割线段 {currentEdgeIndex} ({a}->{b})"); } + // 判断是否前进或后退段 + float switchThreshold = 0.05f; + + // 往前走(靠近 b 点且还有下一段) + if (Vector2.Distance(projected, b) < switchThreshold && currentEdgeIndex < edgePoints.Count - 2) + { + currentEdgeIndex++; + } + // 往后退(靠近 a 点且还有上一段) + else if (Vector2.Distance(projected, a) < switchThreshold && currentEdgeIndex > 0) + { + currentEdgeIndex--; + } +} + + private int FindNearestEdgePointIndex(Vector2 pos, out float minDist) { int nearestIndex = 0;