如何在一定时间内扩展对角线渲染线

时间:2017-11-06 20:40:01

标签: c# unity3d

我通过将以下脚本附加到空游戏对象来创建对角线渲染器。如何将两端的线延长一半的长度,如何沿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<LineRenderer>();
    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);

    }
}

enter image description here

2 个答案:

答案 0 :(得分:3)

将问题分解成碎片:

1 。双方按x单位扩展:

这是通过Ray类完成的。从RaystartPoint变量创建新的endPoint实例,然后使用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 。对于动画/移动它,请使用协程和Time.deltaTime。每帧增加一个变量Time.deltaTime,然后使用Vector3.Lerp来触发from和to值。

例如,请参阅this函数。

两者合并后,下面是一个完整的功能,可以随着时间的推移扩展两条线:

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;
}

<强> USAGE

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<LineRenderer>();
    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 单位延伸至两侧。

答案 1 :(得分:2)

这是基本的矢量数学。

你有一条线(从头到尾):

 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);