unity3d 如何使线渲染器保持平面?

jexiocij  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(195)

我正在两个navmesh代理之间绘制一个线渲染器,并为其分配一个方向箭头纹理。但问题是它垂直地站在我的道路结构的顶部。我需要平躺下来。

在两个代理之间绘制线的代码:

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]);
        
    }
}
     
}

以下是我对线条渲染器的设置:

ymdaylpp

ymdaylpp1#

你可以使用一个小技巧:

  • LineRenderer设置为position = Vector3.zero
  • 设置Use World Space = false-〉将使用局部空间位置
  • 将线旋转到x = 90°
  • 最后,您必须稍微改变位置,并翻转ZY

比如

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);
}

相关问题