A
A
artur_agishev2021-01-23 07:59:35
Unity
artur_agishev, 2021-01-23 07:59:35

How to draw the trajectory (on which it moved) of the movement of an object in Unity 2D?

I have a red circle, which should leave lines after itself, that is, the trajectory along which it moved. I wrote a small script and attached it to it, but something is not working.
My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Траектория : MonoBehaviour
{
    public Material mat;
    Vector2 start, end;

    void OnRenderObject()
    {
        end = transform.position;
        GL.PushMatrix();
        mat.SetPass(0);
        GL.LoadOrtho();

        GL.Begin(GL.LINES);
        GL.Color(Color.red);
        GL.Vertex(start);
        GL.Vertex(end);
        GL.End();

        GL.PopMatrix();

        start = end;       
    }

    void Start()
    {
        start = transform.position;
    }
}

What is available now:
600bacf504288848403877.png
What is approximately needed:
600bad1e9027b017982601.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2021-01-23
@freeExec

Trail Renderer

H
Haddle Haddle, 2021-01-24
@Haddle

Already done, or help?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question