- Home /
Question by
UTus · Sep 21, 2020 at 12:37 AM ·
pathfollowing
how to draw path
I specify a path and I want to draw the corresponding path with the mouse.
Is there a good way?
All answers are welcome :)
Comment
Answer by N-8-D-e-v · Sep 21, 2020 at 01:25 AM
Check out Line Renderers
I succeeded in drawing the line. However, it did not show a curve and did not implement path movement.
public class DrawLine : ObjectBase
{
[Header("Line")]
public GameObject linePrefab;
[Header("Line Pooling Size")]
public int poolingSize;
private bool isDrawingStart;
private Vector2 original$$anonymous$$ousePos;
private Vector2 current$$anonymous$$ousePos;
private List<GameObject> line_List = new List<GameObject>();
private LineRenderer currentDrawingLineRenderer;
protected override IEnumerator OnAwakeCoroutine()
{
return base.OnAwakeCoroutine();
}
private void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0)) OnClick_DrawStart();
if (Input.Get$$anonymous$$ouseButton(0) && isDrawingStart) Drawing();
if (Input.Get$$anonymous$$ouseButtonUp(0)) OnClick_DrawEnd();
}
private void CreateLine()
{
if(line_List.Count < poolingSize)
{
GameObject temp = Instantiate(linePrefab);
SetLineRendererPosition(temp);
line_List.Add(temp);
}
else
{
SetLineRendererPosition(line_List[0]);
ObjectPooling$$anonymous$$anager.$$anonymous$$oveFirstToEnd(line_List);
}
}
private void SetLineRendererPosition(GameObject line)
{
currentDrawingLineRenderer = line.GetComponent<LineRenderer>();
currentDrawingLineRenderer.SetPosition(0, original$$anonymous$$ousePos);
currentDrawingLineRenderer.SetPosition(1, original$$anonymous$$ousePos);
}
private void Drawing()
{
current$$anonymous$$ousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
currentDrawingLineRenderer.SetPosition(1, current$$anonymous$$ousePos);
}
#region Event
private void OnClick_DrawStart()
{
isDrawingStart = true;
original$$anonymous$$ousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
CreateLine();
}
private void OnClick_DrawEnd()
{
isDrawingStart = false;
}
#endregion Event
}
캡처.png
(7.5 kB)
$$anonymous$$y bad, I didn't realize that's what you wanted, Trail renderers are what you're looking for
Your answer

Follow this Question
Related Questions
how to create 'pathfollow-no collision' code 2d? 1 Answer
How to add more walking path elements 0 Answers
How to prevent non-collider-objects from touching each other? 1 Answer
Problem with the climbing of stairs ( character piloted by coordinates) 0 Answers
iTween path constrained character: force original orientation? 0 Answers