unity 如何使用LineRenderer 動(dòng)態(tài)劃線
我就廢話不多說了,大家還是直接看代碼吧~
private LineRenderer line1;
//畫線
line1 = this.gameObject.AddComponent<LineRenderer>();
//只有設(shè)置了材質(zhì) setColor才有作用
line1.material = new Material(Shader.Find("Particles/Additive"));
line1.SetVertexCount(2);//設(shè)置兩點(diǎn)
line1.SetColors(Color.yellow, Color.red); //設(shè)置直線顏色
line1.SetWidth(5f, 10f);//設(shè)置直線寬度
//設(shè)置指示線的起點(diǎn)和終點(diǎn)
line1.SetPosition(0, A.transform.position);
line1.SetPosition(1, B.transform.position);
Destroy(this.gameObject.GetComponent<LineRenderer>());
補(bǔ)充:Unity LineRenderer繪制物體行走路線
我是用的角色控制器(Character Controller)+LineRenderer做的
下面是代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTest : MonoBehaviour
{
public GameObject clone;//這是個(gè)空物體 只添加了一個(gè)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);//設(shè)置顏色
line.SetWidth(0.2f, 0.1f);//設(shè)置寬度
}
void Update()
{
time += Time.deltaTime;
if (time>0.1)//每0.1秒繪制一次
{
time = 0;
pos.Add(transform.position);//添加當(dāng)前坐標(biāo)進(jìn)鏈表
path = pos.ToArray();//轉(zhuǎn)成數(shù)組
}
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)//有數(shù)據(jù)時(shí)候再繪制
{
line.SetVertexCount(path.Length);//設(shè)置頂點(diǎn)數(shù)
line.SetPositions(path);//設(shè)置頂點(diǎn)位置
}
}
}
補(bǔ)充:Unity組件 — LineRenderer動(dòng)態(tài)添加碰撞
基礎(chǔ)知識:
選中要添加組件的gameObject,在Inspector面板,點(diǎn)擊“Add Component”按鈕,選中LineRenderer組件,添加。

cast Shadows : 蒙上陰影
Receive Shadows : 是否接受陰影
Dynamic Occludee : 是否動(dòng)態(tài)遮罩
Materials 中的屬性:
size : 材質(zhì)球的數(shù)量
Element : 具體的材質(zhì)球
Positions 中的屬性:
size : 位置的數(shù)量
Element : 具體的位置
Use World Space : 是否使用世界坐標(biāo)系,還是使用相對坐標(biāo)系
Width : 線條的寬度
Color : 線條的顏色,注:如果沒有賦值材質(zhì),無論怎樣改變Color的值,顏色都不會(huì)有改變。
Corner Vertices : 可形成線條的圓角效果
End Cap Vertices : 影響線條的兩端的圓角效果。
注:當(dāng)line Renderer擁有了材質(zhì),可以通過修改Color來改變顏色。當(dāng)時(shí)當(dāng)修改了Color后,line的顏色沒有改變,應(yīng)該是Material和Color屬性結(jié)合不好。將Material修改為Sprites/Default,Color的顏色就可以成功的顯示在line上面了。
動(dòng)態(tài)添加碰撞器(Polygon Collider2D)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseTrack : MonoBehaviour
{
/// <summary>
/// 獲取LineRenderer組件
/// </summary>
[Header("獲得LineRenderer組件")]
public LineRenderer lineRenderer;
//獲得鼠標(biāo)跟蹤位置
private Vector3[] mouseTrackPositions = new Vector3[20];
private Vector3 headPosition; //頭位置
private Vector3 lastPosition; //尾位置
private int positionCount = 0; //位置計(jì)數(shù)
[Header("設(shè)置多遠(yuǎn)距離記錄一個(gè)位置")]
public float distanceOfPositions = 0.01f;
private bool firstMouseDown = false; //第一次鼠標(biāo)點(diǎn)擊
private bool mouseDown = false; //鼠標(biāo)點(diǎn)擊
PolygonCollider2D polygonCollider; //添加多邊形碰撞
void Start()
{
polygonCollider = gameObject.GetComponent<PolygonCollider2D>();
}
void Update()
{
//鼠標(biāo)點(diǎn)擊的時(shí)候
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;
//頭坐標(biāo)
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));
//判斷頭坐標(biāo)到尾坐標(biāo)的距離是否大于記錄點(diǎn)位
if (Vector3.Distance(headPosition, lastPosition) > distanceOfPositions)
{
//用于保存位置
SavePosition(headPosition);
positionCount++;
}
lastPosition = headPosition;
}
//設(shè)置線性渲染器的位置
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;
//創(chuàng)建碰撞路徑
List<Vector2> colliderPath = GetColliderPath(mouseTrackPositions);
polygonCollider.SetPath(0, colliderPath.ToArray());
}
//計(jì)算碰撞體輪廓
float colliderWidth;
List<Vector2> pointList2 = new List<Vector2>();
List<Vector2> GetColliderPath(Vector3[] pointList3)
{
//碰撞體寬度
colliderWidth = lineRenderer.startWidth;
//Vector3轉(zhuǎn)Vector2
pointList2.Clear();
for (int i = 0; i < pointList3.Length; i++)
{
pointList2.Add(pointList3[i]);
}
//碰撞體輪廓點(diǎn)位
List<Vector2> edgePointList = new List<Vector2>();
//以LineRenderer的點(diǎn)位為中心, 沿法線方向與法線反方向各偏移一定距離, 形成一個(gè)閉合且不交叉的折線
for (int j = 1; j < pointList2.Count; j++)
{
//當(dāng)前點(diǎn)指向前一點(diǎn)的向量
Vector2 distanceVector = pointList2[j - 1] - pointList2[j];
//法線向量
Vector3 crossVector = Vector3.Cross(distanceVector, Vector3.forward);
//標(biāo)準(zhǔn)化, 單位向量
Vector2 offectVector = crossVector.normalized;
//沿法線方向與法線反方向各偏移一定距離
Vector2 up = pointList2[j - 1] + 0.5f * colliderWidth * offectVector;
Vector2 down = pointList2[j - 1] - 0.5f * colliderWidth * offectVector;
//分別加到List的首位和末尾, 保證List中的點(diǎn)位可以圍成一個(gè)閉合且不交叉的折線
edgePointList.Insert(0, down);
edgePointList.Add(up);
//加入最后一點(diǎn)
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);
}
}
//返回點(diǎn)位
return edgePointList;
}
//設(shè)置線條渲染器位置
private void SetLineRendererPosition(Vector3[] position)
{
lineRenderer.SetPositions(position);
}
//用于清除碰撞和線性渲染
void ClearColliderAndLineRenderer()
{
if (polygonCollider)
{
polygonCollider.enabled = false;
}
lineRenderer.positionCount = 0;
}
}
效果圖:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- Unity3D實(shí)現(xiàn)播放gif圖功能
- Unity 如何獲取鼠標(biāo)停留位置下的物體
- Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作
- 解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑
- unity AudioSource播放完聲音后要執(zhí)行的函數(shù)或條件操作
- unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
- Unity3d 使用Gizmos畫一個(gè)圓圈
- Unity 通過LineRenderer繪制兩點(diǎn)之間的直線操作
- Unity 實(shí)現(xiàn)給物體替換材質(zhì)球
- Unity解析gif動(dòng)態(tài)圖操作
相關(guān)文章
.NET單點(diǎn)登陸的實(shí)現(xiàn)方法及思路
這篇文章介紹了.NET單點(diǎn)登陸的實(shí)現(xiàn)方法及思路,有需要的朋友可以參考一下,希望對你有所幫助2013-07-07

