Home Game Development unity – How to cut back jaggedness of free-drawing with Line Renderer

unity – How to cut back jaggedness of free-drawing with Line Renderer

0
unity – How to cut back jaggedness of free-drawing with Line Renderer

[ad_1]

Like many different folks, I’ve been making an attempt 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 a answer that was talked about on this Unity discussion board thread.

My downside is that sharp turns when free drawing causes artifacts that I’d wish to keep away from, just like the one proven right here:

Example of artifacts

If you take a look at the final put up in that thread, the person says they’ve solved the issue by:

Converting the road into small 2-point segments after which group all of them in the identical mum or dad. This means the rendering appears easy with none sharp edge like earlier than and the efficiency of the entire thing is identical.

This appears like an incredible answer, however I’m making an attempt to determine learn how to implement it.

Here, I’m making an attempt to create the two level segments by solely calling the AddPointToLine each 2 seconds or so, however it would not appear to work. I really feel like that is the incorrect method to go, however actually, for those who break down what this person is saying, it looks as if:

  • They do not add a degree based mostly on the gap between them, they add one other level after a specified time frame.

  • Then maybe the final level turns into the start of the subsequent line? That could possibly be what I’m lacking right here…

  • Then in some way including a typical mum or dad retains all of it collectively.

Here are my courses:

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

    personal void FinishLine()
    {
        // sign that the road needs to be nullified right here
        if (livelyLine != null)
        {
            // finish it right here
            livelyLine = null;
        }
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0)) // this appears nice
        {
            GameObject newLine = Instantiate(linePrefab);
            livelyLine = newLine.GetComponent<Line>();
        }

        if (Input.GetMouseButtonUp(0)) // this appears nice
        {
            livelyLine = null;
        }

        if (livelyLine != null) 
        {
            // this merely sends the present mouse place by
            mousePos = Camera.major.ScreenToWorldPoint(Input.mousePosition);
            // livelyLine.UpdateLine(mousePos);
            InvokeRepeating("AddPointToLine", 0f, 2f);
        }
    }

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

Line class:

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

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

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

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

        if (Vector2.Distance(factors.Last(), place) > 0.1f) // this is perhaps inflicting a difficulty, I've additionally commented it out, however it nonetheless would not work accurately.
        {
            SetPoint(place);
        }
    }

    void SetPoint(Vector2 level)
    {
        factors.Add(level);
        lineRenderer.placeCount = factors.Count;
        lineRenderer.SetPosition(factors.Count - 1, level);
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here