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
93 views
in Technique[技术] by (71.8m points)

c# - Unity 2D - How to I connect one point to another with a line in code?

Hi I want to make a line which stretches from a starting point to a end point, so I have a point which has a random position and another point at 0, 0 I want a line to connect them how would I do that? I have tried nothing so far because I don't know what to use. I would also like to be able to change the texture line or a custom line. I want to do this all in 2D and in unity c#

question from:https://stackoverflow.com/questions/65649242/unity-2d-how-to-i-connect-one-point-to-another-with-a-line-in-code

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

1 Reply

0 votes
by (71.8m points)

You can use a Line Renderer Component attached to some gameobject, then you assign the desired material and you can via code specify the positions of the points:

LineRenderer lineRenderer = GetComponent<LineRenderer>();
Vector3[] positions = {Vector3.zero, someOtherVector3};
lineRenderer.SetPositions(positions);

Note: you can use as many points as you need. Note 2: No matter that your game is 2D, you can use Vector3, just keep in mind that the z is 0.


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

1.4m articles

1.4m replys

5 comments

56.9k users

...