- Home /
Question by
Crakker1337 · Dec 28, 2020 at 03:41 PM ·
c#unity 5line renderer
How to draw a line like this? (3D)
(3D) How can I do a mechanic like this? At first I've tried to do raycast that follows my mouse but I failed. So I need to have a line, when I move my mouse it will follow it, but the length is until it reaches any object and in the end of the line there will be any sprite (on screenshot there is a crosshair or something)
screenshot-1.png
(208.1 kB)
Comment
Best Answer
Answer by GoldDeniel · Dec 28, 2020 at 04:13 PM
If you want to do it with a line renderer, then set the line renderer's first position to the A position, and set the second position to the B position. This will cause the line renderer draw a line between those two points.
public LineRenderer lineRenderer;
Vector3 aPosition = Vector3.zero; // GameObject's tansform.position
Vector3 bPosition = new Vector3(1,1,1);// Target's transform.position
private void Awake()
{
lineRenderer = GetComponent<LineRenderer>(); // Gets the LineRenderer component from the GameObject that the script is on
}
private void Update()
{
DrawLine();
}
private void DrawLine()
{
lineRenderer.SetPosition(0, aPosition); // Sets the line's first position to the first GameObject
lineRenderer.SetPosition(1, bPosition); // Sets the line's second position to the target's position
}
I hope it works.
You can set the line material, the width and other parameters in the inspector.