如何修改协程实现从中间向两侧展开的弧线动画?
How to Modify Arc Animation to Expand from Center to Both Sides?
Hey there! Let's tweak your coroutine to make the arc grow outward from its center instead of drawing from one end to the other. The core idea is to start at the midpoint of your intended arc and add points to both left and right sides simultaneously as the animation runs.
Key Adjustments to Your Original Code
Your current code draws the arc in one direction by incrementing a single angle value each frame. To get the "expand from center" effect, we need to:
- Start at the midpoint angle of your full arc
- Calculate two points per frame: one offset to the left of the midpoint, one to the right
- Update the LineRenderer to add these points to both ends of the existing line
Modified Coroutine Code
IEnumerator AnimateArcFromCenter(float duration) { float waitDur = duration / pts; // Calculate the middle angle of your total arc (adjust startAngle to your arc's starting edge) float midAngle = startAngle + totalAngle / 2f; // Initialize with the center point of the arc first arcLineRend.positionCount = 1; arcLineRend.SetPosition(0, new Vector2( center.x + radius * Mathf.Cos(midAngle * Mathf.Deg2Rad), center.y + radius * Mathf.Sin(midAngle * Mathf.Deg2Rad) )); for (int i = 1; i <= pts; i++) { // Calculate how far we've expanded from the midpoint this step float angleOffset = (float)totalAngle / pts * i / 2f; // Compute left and right points relative to the midpoint Vector2 leftPoint = new Vector2( center.x + radius * Mathf.Cos((midAngle - angleOffset) * Mathf.Deg2Rad), center.y + radius * Mathf.Sin((midAngle - angleOffset) * Mathf.Deg2Rad) ); Vector2 rightPoint = new Vector2( center.x + radius * Mathf.Cos((midAngle + angleOffset) * Mathf.Deg2Rad), center.y + radius * Mathf.Sin((midAngle + angleOffset) * Mathf.Deg2Rad) ); // Update LineRenderer: add left point to the start, right point to the end arcLineRend.positionCount += 2; // Shift existing points right to make space for the new left point for (int j = arcLineRend.positionCount - 1; j >= 1; j--) { arcLineRend.SetPosition(j, arcLineRend.GetPosition(j - 1)); } arcLineRend.SetPosition(0, leftPoint); arcLineRend.SetPosition(arcLineRend.positionCount - 1, rightPoint); yield return new WaitForSeconds(waitDur); } }
Optimized Version (Smoother & More Efficient)
If shifting points every frame feels clunky, precompute all points first then animate by building the line from both ends toward the center:
IEnumerator AnimateArcFromCenterOptimized(float duration) { float waitDur = duration / pts; float midAngle = startAngle + totalAngle / 2f; // Precompute all left and right arc points upfront List<Vector2> leftPoints = new List<Vector2>(); List<Vector2> rightPoints = new List<Vector2>(); for (int i = 0; i <= pts; i++) { float angleOffset = (float)totalAngle / pts * i / 2f; leftPoints.Add(new Vector2( center.x + radius * Mathf.Cos((midAngle - angleOffset) * Mathf.Deg2Rad), center.y + radius * Mathf.Sin((midAngle - angleOffset) * Mathf.Deg2Rad) )); rightPoints.Add(new Vector2( center.x + radius * Mathf.Cos((midAngle + angleOffset) * Mathf.Deg2Rad), center.y + radius * Mathf.Sin((midAngle + angleOffset) * Mathf.Deg2Rad) )); } // Start with the center point arcLineRend.positionCount = 1; arcLineRend.SetPosition(0, leftPoints[0]); // Same as rightPoints[0] for (int i = 1; i <= pts; i++) { arcLineRend.positionCount = 1 + 2 * i; // Fill left side (outer to center) for (int j = 0; j < i; j++) { arcLineRend.SetPosition(j, leftPoints[i - j]); } // Keep the center point in the middle arcLineRend.SetPosition(i, leftPoints[0]); // Fill right side (center to outer) for (int j = 0; j < i; j++) { arcLineRend.SetPosition(i + 1 + j, rightPoints[j + 1]); } yield return new WaitForSeconds(waitDur); } }
Quick Notes
- Ensure
startAngleis defined (this is the starting edge angle of your original arc; e.g., for a 180° arc from 0° to 180°,startAngle = 0andtotalAngle = 180) - The
ptsvariable controls animation smoothness—higher values mean more steps and a smoother arc - Both versions work with your existing
center,radius, andarcLineRendreferences
内容的提问来源于stack exchange,提问作者TenOutOfTen
相关产品推荐
相关产品推荐

