Thursday, October 27, 2022
HomeGame Developmentunity - Learn how to implement an answer for modification of Line...

unity – Learn how to implement an answer for modification of Line Renderer


Like many different individuals, I have been attempting to keep away from the jaggedness of line renderer when free drawing. This query has been requested many instances, however my query is about implementing an answer that was talked about in a Unity discussion board thread.

My downside is that sharp turns when free drawing causes artifacts that I would wish to keep away from, just like the one proven right here: https://discussion board.unity.com/threads/line-renderer-renderization-issue.699608/.

When you have a look at the final publish right here, the consumer says they’ve solved the issue by: “Changing the road into small 2-point segments after which group all of them in the identical mum or dad. This fashion the rendering seems to be easy with none sharp edge like earlier than and the efficiency of the entire thing is similar.” This seems to be like an ideal answer, however I am attempting to determine find out how to implement it.

Right here, I am attempting to create the two level segments by solely calling the AddPointToLine each 2 seconds or so, nevertheless it would not appear to work. I really feel like that is the mistaken approach to go, however actually, in the event you break down what this man is saying, it looks as if:

  • He would not add some extent primarily based on the gap between them, he provides one other level after a specified time period
  • Then maybe the final level turns into the start of the subsequent line? That might be what I am lacking right here…
  • Then someway including a typical mum or dad retains all of it collectively.

Listed below are my courses:

public class DrawManager : MonoBehaviour
{
    public GameObject linePrefab;
    Line activeLine;
    personal Vector2 mousePos;
    

    personal void EndLine()
    {
        // sign that the road ought to be nullified right here
        if (activeLine != null)
        {
            // finish it right here
            activeLine = null;
        }
    }

    void Replace()
    {
        if (Enter.GetMouseButtonDown(0)) // this appears tremendous
        {
            GameObject newLine = Instantiate(linePrefab);
            activeLine = newLine.GetComponent<Line>();
        }

        if (Enter.GetMouseButtonUp(0)) // this appears tremendous
        {
            activeLine = null;
        }

        if (activeLine != null) 
        {
            // this merely sends the present mouse place by way of
            mousePos = Digicam.essential.ScreenToWorldPoint(Enter.mousePosition);
            // activeLine.UpdateLine(mousePos);
            InvokeRepeating("AddPointToLine", 0f, 2f);
        }
    }

    void AddPointToLine()
    {
        activeLine.UpdateLine(mousePos);
    }
}

Line class:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing System.Linq;

public class Line : MonoBehaviour
{
    public LineRenderer lineRenderer;
    Checklist<Vector2> factors;

    public delegate void EndLine();
    public static occasion EndLine MakeSegment;

    public void UpdateLine(Vector2 place)
    {
        // that is the place you'd create the road segments and group them
        if (factors == null)
        {
            factors = new Checklist<Vector2>();
            SetPoint(place);
            return;
        }

        if (Vector2.Distance(factors.Final(), place) > 0.1f) // this may be inflicting a difficulty, I've additionally commented it out, nevertheless it nonetheless would not work appropriately.
        {
            SetPoint(place);
        }
    }

    void SetPoint(Vector2 level)
    {
        factors.Add(level);
        lineRenderer.positionCount = factors.Depend;
        lineRenderer.SetPosition(factors.Depend - 1, level);
    }
}
````

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments