Home > Software engineering >  how to make line renderer stay flat on the surface?
how to make line renderer stay flat on the surface?

Time:10-28

I am drawing a line renderer between two navmesh agents and assigning a direction arrow texture to it. But the problem is that it is standing vertically on top of my road structure. I need to make a lie down flat.

enter image description here

The code for drawing lines between two agents:

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

public class ParkingLevel : MonoBehaviour
{

[Space(10)]
[Header("For Path Rendering")]
public Transform targetAgent;
public NavMeshAgent agent_ParkingPoint;
public LineRenderer line;

public static ParkingLevel Instance;

void OnEnable()
{
    if (Instance == null)
    {
        Instance = this;
        line.startWidth = 3;
        line.endWidth = 3;
       
        return;
    }
}


void OnDisable()
{
    Instance = null;
}

void LateUpdate()
{
    GetPath();
}

public void GetPath()
{
    targetAgent = PlayerActivitiesManager.Instance.busAgent.transform;

    line.SetPosition(0, agent_ParkingPoint.gameObject.transform.position);

    agent_ParkingPoint.SetDestination(targetAgent.position);

    DrawPath(agent_ParkingPoint.path);

    agent_ParkingPoint.isStopped = true;
}


private void DrawPath(NavMeshPath path)
{
    if (path.corners.Length < 2)
        return;

    line.positionCount = path.corners.Length;

    for (var i = 1; i < path.corners.Length; i  )
    {
        line.SetPosition(i, path.corners[i]);
        
    }
}
     
}

Here are my settings for the line renderer: enter image description here

CodePudding user response:

You could use a little trick:

  • Set the LineRenderer to position = Vector3.zero
  • Set Use World Space = false -> will use local space positions
  • Rotate the line to x = 90°
  • Finally now you have to alter the positions slightly and flip Z and Y axis

so something like e.g.

void OnEnable()
{
    if (Instance == null)
    {
        Instance = this;
        line.startWidth = 3;
        line.endWidth = 3;
        line.useWorldSpace = false;
        var lineTransform = line.transform;
        lineTransform.parent = null;
        lineTransform.position = Vector3.zero;
        lineTransform.localScale = Vector3.one;
        line.transform.rotation = Quaternion.Euler(90, 0, 0);
    }
}

private void DrawPath(NavMeshPath path)
{
    if (path.corners.Length < 2)
    {
        line.enabled = false;
        return;
    }

    line.enabled = true;

    var flippedPositions = new Vector3[path.corners.Length];
    var firstPosition = agent_ParkingPoint.transform.position;
    var fistFlippedPosition = new Vector3(firstPosition.x, firstPosition.z, firstPosition.y);

    flippedPositions[0] = fistFlippedPosition;

    for (var i = 1; i < path.corners.Length; i  )
    {
        var p = path.corners[i];
        flippedPositions[i] = new Vector3(p.x, p.z, p.y);
    }

    line.positionCount = flippedPositions.Length;

    line.SetPositions(flippedPositions);
}
  • Related