Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

c# - How to extend diagonal line renderer line over a certain duration

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

    }
}

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is basic vector math.

You have the line with (from end to start):

 Vector3 v = start - end;

and then you extend on each side by half if it:

 extensionA = start + (v * 0.5f);
 extensionB = end + (v * -0.5f);

If you need to extend by 1 then normalize:

 Vector3 v = (start - end).normalized;
 extensionA = start + v;
 extensionB = end + (v * -1f);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...