如何在一定时间内扩展对角线渲染器线
我通过将以下脚本附加到空游戏对象来创建对角线渲染器。 如何将两端的线延长一半长度,如何沿x轴延长线1个单位? 两者都在一段时间内。
public class DiagonalLine : MonoBehaviour { bool firstLineComplete = false; LineRenderer diagLine; public Vector3 startPoint = new Vector3 (0, 0, 0); public Vector3 endPoint = new Vector3 (1.0f, 1.0f, 0); public float lineDrawSpeed; // Use this for initialization void Start () { diagLine = gameObject.AddComponent(); diagLine.material = new Material (Shader.Find ("Sprites/Default")); diagLine.startColor = diagLine.endColor = Color.green; diagLine.startWidth = diagLine.endWidth = 0.15f; diagLine.SetPosition (0, startPoint); diagLine.SetPosition (1, endPoint); } }
将问题分解成碎片:
1,双方按x单位扩展:
这是通过Ray
类完成的。 从startPoint
和endPoint
变量创建一个新的Ray
实例,然后使用Ray.GetPoint
函数扩展该行。 您必须在两侧执行此操作才能获得新的扩展行。
一个简单的Ray
类包装器来简化这个:
Vector3 extendLine(Vector3 startPoint, Vector3 endPoint, ExtendDirection extendDirection, float extendDistance) { Ray ray = new Ray(); //Start if (extendDirection == ExtendDirection.START_POINT) { ray.origin = startPoint; ray.direction = startPoint - endPoint; } //End else if (extendDirection == ExtendDirection.END_POINT) { ray.origin = endPoint; ray.direction = endPoint - startPoint; } //Extend Vector3 newUnityPoint = ray.GetPoint(extendDistance); //Debug.DrawLine(ray.origin, newUnityPoint, Color.blue); return newUnityPoint; } public enum ExtendDirection { START_POINT, END_POINT }
延伸到左端
Vector3 newStartPos = extendLine(startPoint, endPoint, ExtendDirection.START_POINT, 4); diagLine.SetPosition(0, newStartPos);
延伸到右端
Vector3 newEndPos = extendLine(startPoint, endPoint, ExtendDirection.END_POINT, 4); diagLine.SetPosition(1, newEndPos);
2.对于动画/移动它,请使用coroutine和Time.deltaTime
。 使用Time.deltaTime
每帧增加一个变量,然后使用Vector3.Lerp
来触发from和to值。
例如,请参阅此function。
两者结合使用,下面是一个完整的function,可以随时间延长两条线:
bool isRunning = false; IEnumerator extentLineOverTime(LineRenderer targetLineRenderer, float extendDistance, float duration) { //Calculate Left from extension length Vector3 fromValLeftPos = targetLineRenderer.GetPosition(0); //Calculate Right from extension length Vector3 fromValRightPos = targetLineRenderer.GetPosition(1); //Calculate Left to extension length Vector3 newLeftPos = extendLine(fromValLeftPos, fromValRightPos, ExtendDirection.START_POINT, extendDistance); //Calculate Right to extension length Vector3 newRightPos = extendLine(fromValLeftPos, fromValRightPos, ExtendDirection.END_POINT, extendDistance); //Make sure there is only one instance of this function running if (isRunning) { yield break; ///exit if this is still running } isRunning = true; float counter = 0; while (counter < duration) { counter += Time.deltaTime; //Move to left overtime Vector3 tempLeftPos = Vector3.Lerp(fromValLeftPos, newLeftPos, counter / duration); targetLineRenderer.SetPosition(0, tempLeftPos); //Move to Right overtime Vector3 tempRightPos = Vector3.Lerp(fromValRightPos, newRightPos, counter / duration); targetLineRenderer.SetPosition(1, tempRightPos); yield return null; } isRunning = false; }
用法 :
LineRenderer diagLine; public Vector3 startPoint = new Vector3(0, 0, 0); public Vector3 endPoint = new Vector3(1.0f, 1.0f, 0); // Use this for initialization void Start() { diagLine = gameObject.AddComponent(); diagLine.material = new Material(Shader.Find("Sprites/Default")); diagLine.startColor = diagLine.endColor = Color.green; diagLine.startWidth = diagLine.endWidth = 0.15f; diagLine.SetPosition(0, startPoint); diagLine.SetPosition(1, endPoint); //Extend Line Over time StartCoroutine(extentLineOverTime(diagLine, 4, 3)); }
StartCoroutine(extentLineOverTime(diagLine, 4, 3));
将在3秒内将线路从两侧延伸4个单元。
这是基本的矢量数学。
你有一条线(从头到尾):
Vector3 v = start - end;
然后你将每一边延伸一半:
extensionA = start + (v * 0.5f); extensionB = end + (v * -0.5f);
如果需要延长1,则标准化:
Vector3 v = (start - end).normalized; extensionA = start + v; extensionB = end + (v * -1f);