- Home /
Draw line using line renderer on mouse position wtihout "use world space"
Hi, I am drawing line on mouse position using Line Renderer component and its working but when i turn off "use world space" line do not follow mouse. Note: I turned off "use world space" because i need to move that line!!
Answer by tormentoarmagedoom · Apr 12, 2018 at 11:28 AM
Good day.
Then Maybe you need to create a emptyGameobject, with a script that makes the object be in the mouse position all time.
Then, when you need to move the line, just make the object stop following the mouse and move it at your desire.
PD: There is a component called TrailRenderer. Is exactly like a LineREnderer, but it creates a line from positions of the object containing the component. I recommend you to try it, because make very more easy to generate lines that follows something (like the mouse)
PD2: @x4637x solution semms to be correct, try it.
If helped, accept the answer and close the question! Bye!
Answer by x4637x · Apr 12, 2018 at 11:42 AM
You can try using this simple function to convert your LineRenderer :
private void KeepLine(LineRenderer line)
{
if (!line.useWorldSpace) return;
Vector3[] linePoints = new Vector3[line.positionCount];
line.GetPositions(linePoints);
for (int i = 0; i < linePoints.Length; i++)
{
linePoints[i] = line.transform.InverseTransformPoint(linePoints[i]);
}
line.SetPositions(linePoints);
line.useWorldSpace = false;
}
How can I use this could you please explain me bit more? Im giving mousePosition in Line.SetPosition..
Just call $$anonymous$$eepLine(Line);
when you need to turn off "use world space" in your code.