如何在一定时间内延长对角线渲染器线 [英] How to extend diagonal line renderer line over a certain duration

查看:159
本文介绍了如何在一定时间内延长对角线渲染器线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过将以下脚本附加到一个空的游戏对象上来创建了对角线渲染器.如何在两端将线延长一半的长度,如何在x轴上将线延长1个单位?两者都超过了一定时间.

I created a diagonal line renderer by attaching the following script to an empty game object. How can I extend the line at both ends for a half its length and how can I also extend the line by say 1 unit along the x-axis? Both over a certain period of time.

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

    }
}

推荐答案

将您的问题分解成碎片:

Break your problem into pieces:

1 .在两边以x个单位延长行:

1.Extend line by x units on both sides:

这是通过 Ray 类完成的.从startPointendPoint变量创建一个新的Ray实例,然后使用 Ray.GetPoint 函数以扩展行.您必须在两面都这样做,以获取新的延长线.

This is done with the Ray class. Create a new Ray instance from the startPoint and endPoint variables then use the Ray.GetPoint function to extend the line. You have to do this on both sides to get the new extended lines.

Ray类的简单包装,以简化此操作:

A simple wrapper for the Ray class to simplify this:

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值.


2.For animating/moving it over time, use coroutine and Time.deltaTime. Increment a variable with Time.deltaTime every frame to then use Vector3.Lerp to lerp the from and to value.

例如,参见函数.

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

With both combined, below is a complete function to extend both lines over time:

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<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 个单位.

The StartCoroutine(extentLineOverTime(diagLine, 4, 3)); will extend the line 4 units away from both sides within 3 seconds.

这篇关于如何在一定时间内延长对角线渲染器线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆