private LineRenderer line1;
//畫線
line1 = this.gameObject.AddComponent<LineRenderer>();
//只有設置了材質 setColor才有作用
line1.material = new Material(Shader.Find("Particles/Additive"));
line1.SetVertexCount(2);//設置兩點
line1.SetColors(Color.yellow, Color.red); //設置直線顏色
line1.SetWidth(5f, 10f);//設置直線寬度
//設置指示線的起點和終點
line1.SetPosition(0, A.transform.position);
line1.SetPosition(1, B.transform.position);
Destroy(this.gameObject.GetComponent<LineRenderer>());
補充:Unity LineRenderer繪制物體行走路線
我是用的角色控制器(Character Controller)+LineRenderer做的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTest : MonoBehaviour
{
public GameObject clone;//這是個空物體 只添加了一個LineRenderer組件
public float speed = 5;
public float jumpSpeed = 10f;
public float luodi = 15;
private Vector3 movePos = Vector3.zero;
public CharacterController controller;
private LineRenderer line;
Vector3[] path;
private float time = 0;
List<Vector3> pos=new List<Vector3> ();
void Awake()
{
path = pos.ToArray();//初始化
line = clone.GetComponent<LineRenderer>();//獲得該物體上的LineRender組件
line.SetColors(Color.blue, Color.red);//設置顏色
line.SetWidth(0.2f, 0.1f);//設置寬度
}
void Update()
{
time += Time.deltaTime;
if (time>0.1)//每0.1秒繪制一次
{
time = 0;
pos.Add(transform.position);//添加當前坐標進鏈表
path = pos.ToArray();//轉成數組
}
if (controller.isGrounded)//判斷人物是否落地
{
movePos = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
movePos = transform.TransformDirection(movePos);
movePos *= speed;
if (Input.GetButton("Jump")) {
movePos.y = jumpSpeed;
}
}
movePos.y -= luodi * Time.deltaTime;
controller.Move(movePos * Time.deltaTime);
if (path.Length!=0)//有數據時候再繪制
{
line.SetVertexCount(path.Length);//設置頂點數
line.SetPositions(path);//設置頂點位置
}
}
}
補充:Unity組件 ― LineRenderer動態添加碰撞
基礎知識:
選中要添加組件的gameObject,在Inspector面板,點擊“Add Component”按鈕,選中LineRenderer組件,添加。
cast Shadows : 蒙上陰影
Receive Shadows : 是否接受陰影
Dynamic Occludee : 是否動態遮罩
Materials 中的屬性:
size : 材質球的數量
Element : 具體的材質球
Positions 中的屬性:
size : 位置的數量
Element : 具體的位置
Use World Space : 是否使用世界坐標系,還是使用相對坐標系
Width : 線條的寬度
Color : 線條的顏色,注:如果沒有賦值材質,無論怎樣改變Color的值,顏色都不會有改變。
Corner Vertices : 可形成線條的圓角效果
End Cap Vertices : 影響線條的兩端的圓角效果。
注:當line Renderer擁有了材質,可以通過修改Color來改變顏色。當時當修改了Color后,line的顏色沒有改變,應該是Material和Color屬性結合不好。將Material修改為Sprites/Default,Color的顏色就可以成功的顯示在line上面了。
動態添加碰撞器(Polygon Collider2D)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseTrack : MonoBehaviour
{
/// <summary>
/// 獲取LineRenderer組件
/// </summary>
[Header("獲得LineRenderer組件")]
public LineRenderer lineRenderer;
//獲得鼠標跟蹤位置
private Vector3[] mouseTrackPositions = new Vector3[20];
private Vector3 headPosition; //頭位置
private Vector3 lastPosition; //尾位置
private int positionCount = 0; //位置計數
[Header("設置多遠距離記錄一個位置")]
public float distanceOfPositions = 0.01f;
private bool firstMouseDown = false; //第一次鼠標點擊
private bool mouseDown = false; //鼠標點擊
PolygonCollider2D polygonCollider; //添加多邊形碰撞
void Start()
{
polygonCollider = gameObject.GetComponent<PolygonCollider2D>();
}
void Update()
{
//鼠標點擊的時候
if (Input.GetMouseButtonDown(0))
{
polygonCollider.enabled = true;
lineRenderer.positionCount = 20;
firstMouseDown = true;
mouseDown = true;
}
if (Input.GetMouseButtonUp(0))
{
mouseDown = false;
//ClearColliderAndLineRenderer();
}
OnDrawLine();
firstMouseDown = false;
}
//畫線
private void OnDrawLine()
{
if (firstMouseDown == true)
{
positionCount = 0;
//頭坐標
headPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition + new Vector3(0, 0, 11));
lastPosition = headPosition;
}
if (mouseDown == true)
{
headPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 11));
//判斷頭坐標到尾坐標的距離是否大於記錄點位
if (Vector3.Distance(headPosition, lastPosition) > distanceOfPositions)
{
//用於保存位置
SavePosition(headPosition);
positionCount++;
}
lastPosition = headPosition;
}
//設置線性渲染器的位置
SetLineRendererPosition(mouseTrackPositions);
}
//保存位置
private void SavePosition(Vector3 pos)
{
pos.z = 0;
if (positionCount <= 19)
{
for (int i = positionCount; i < 20; i++)
{
mouseTrackPositions[i] = pos;
}
}
else
{
for (int i = 0; i < 19; i++)
{
mouseTrackPositions[i] = mouseTrackPositions[i + 1];
}
}
mouseTrackPositions[19] = pos;
//創建碰撞路徑
List<Vector2> colliderPath = GetColliderPath(mouseTrackPositions);
polygonCollider.SetPath(0, colliderPath.ToArray());
}
//計算碰撞體輪廓
float colliderWidth;
List<Vector2> pointList2 = new List<Vector2>();
List<Vector2> GetColliderPath(Vector3[] pointList3)
{
//碰撞體寬度
colliderWidth = lineRenderer.startWidth;
//Vector3轉Vector2
pointList2.Clear();
for (int i = 0; i < pointList3.Length; i++)
{
pointList2.Add(pointList3[i]);
}
//碰撞體輪廓點位
List<Vector2> edgePointList = new List<Vector2>();
//以LineRenderer的點位為中心, 沿法線方向與法線反方向各偏移一定距離, 形成一個閉合且不交叉的折線
for (int j = 1; j < pointList2.Count; j++)
{
//當前點指向前一點的向量
Vector2 distanceVector = pointList2[j - 1] - pointList2[j];
//法線向量
Vector3 crossVector = Vector3.Cross(distanceVector, Vector3.forward);
//標准化, 單位向量
Vector2 offectVector = crossVector.normalized;
//沿法線方向與法線反方向各偏移一定距離
Vector2 up = pointList2[j - 1] + 0.5f * colliderWidth * offectVector;
Vector2 down = pointList2[j - 1] - 0.5f * colliderWidth * offectVector;
//分別加到List的首位和末尾, 保證List中的點位可以圍成一個閉合且不交叉的折線
edgePointList.Insert(0, down);
edgePointList.Add(up);
//加入最后一點
if (j == pointList2.Count - 1)
{
up = pointList2[j] + 0.5f * colliderWidth * offectVector;
down = pointList2[j] - 0.5f * colliderWidth * offectVector;
edgePointList.Insert(0, down);
edgePointList.Add(up);
}
}
//返回點位
return edgePointList;
}
//設置線條渲染器位置
private void SetLineRendererPosition(Vector3[] position)
{
lineRenderer.SetPositions(position);
}
//用於清除碰撞和線性渲染
void ClearColliderAndLineRenderer()
{
if (polygonCollider)
{
polygonCollider.enabled = false;
}
lineRenderer.positionCount = 0;
}
}

