Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
Get Length of Trail Renderer

How do I get the length of the whole Trail renderer that is drawn in Unity? For line renderer, we can achieve this using the 1st and 2nd points of the line such as:

var length = (position2 - position1).magnitude;

But unfortunately I could not find the same for trail renderer since it uses multiple points. So how do I achieve this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can iterate through all points using TrailRenderer.GetPositions like e.g.

public static class TrailRendererExtensions
{
    public static float GetTrailLength(this TrailRenderer trailRenderer)
    {
        // You store the count since the created array might be bigger than the actual pointCount

        // Note: apparently the API is wrong?! There it says starting from version 2018.1 the parameter is "out"
        // if you get an error for the "out" anyway or if you use older versions instead use
        //var points = new Vector3[trailRenderer.positionCount]; 
        //var count = trailRenderer.GetPositions(points);
        var count = trailRenderer.GetPositions(out var points);
    
        // If there are not at least 2 points .. well there is nothing to measure
        if(count < 2) return 0f;
    
        var length = 0f;
    
        // Store the first position 
        var start = points[0];

        // Iterate through the rest of positions
        for(var i = 1; i < count; i++)
        {
            // get the current position
            var end = points[i];
            // Add the distance to the last position
            // basically the same as writing
            //length += (end - start).magnitude;
            length += Vector3.Distance(start, end);
            // update the start position for the next iteration
            start = end;
        }
        return length;
    }
}

this is now an extension method so you can simply use it anywhere in your project like

var length = yourTrailRenderer.GetTrailLength();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda