我在兩個導航網格代理之間繪制了一個線渲染器,并為其分配了一個方向箭頭紋理。但問題是它垂直站在我的道路結構頂部。我需要平躺。

在兩個代理之間畫線的代碼:
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]);
}
}
}
這是我對線渲染器的設定:

uj5u.com熱心網友回復:
你可以使用一個小技巧:
- 設定
LineRenderer為position = Vector3.zero - Set
Use World Space = false-> 將使用本地空間位置 - 將線旋轉到
x = 90° - 最后現在你必須稍微改變位置并翻轉
Z和Y軸
所以像例如
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/522423.html
上一篇:Intellisense為一個專案作業,但不能在同一臺機器上為另一個專案作業
下一篇:GPU實體化網格的剔除蒙版
