You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Unity Line Renderer保持贴合地面平铺显示?

问题:Line Renderer垂直立在道路上方,需平铺贴合地面

我在两个NavMesh Agent之间用Line Renderer绘制带方向箭头纹理的路径,但当前路径垂直立在道路结构上方,需要让它平铺贴合地面。

路径垂直立起的效果

绘制路径的代码

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

public class ParkingLevel : MonoBehaviour
{
    [Space(10)]
    [Header("路径渲染相关")]
    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]);
            
        }
    }
}

Line Renderer 当前设置

Line Renderer设置界面


解决方法

1. 统一路径点的Y轴高度

NavMesh路径点可能自带Y轴偏移,直接将所有路径点的Y轴高度修正为地面高度即可贴合地面:
修改DrawPath方法:

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

    line.positionCount = path.corners.Length;

    // 根据场景地面高度设置固定值,或用射线动态检测地面高度
    float groundY = 0f; 

    for (var i = 0; i < path.corners.Length; i++)
    {
        Vector3 adjustedPos = path.corners[i];
        adjustedPos.y = groundY; // 强制将Y轴对齐地面
        line.SetPosition(i, adjustedPos);
    }
}

2. 调整Line Renderer空间设置

在Line Renderer组件中:

  • 将空间(Space)选项改为世界空间(World Space)
  • 勾选使用世界空间(Use World Space),确保路径坐标完全贴合世界坐标系,避免局部空间导致的垂直偏移

3. 修正材质纹理的平铺方向

选中Line Renderer绑定的材质:

  • 将**主纹理(Main Texture)的平铺(Tiling)**中X轴数值调大(比如设为10),让箭头纹理沿路径方向重复平铺
  • 确保Shader选择适合Line Renderer的类型(如Sprites/Default或Unlit/Texture),开启纹理平铺功能

内容的提问来源于stack exchange,提问作者Farrukh Sajjad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 18:10:30