LineRenderer组建实现激光效果

在射击游戏中狙击一般都有一个红外线的效果。比如

鼠标指向哪个方向。就在哪个方向发射一条激光。这个用到了LineRenderer组建

我这里用射线检测。

创建一个圆柱体,添加LineRenderer组建。编写代码

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class ttw : MonoBehaviour
 5 {
 6     LineRenderer reader;
 7     RaycastHit hit;
 8     Ray ray;
 9     void Awake()
10     {
11         reader = GetComponent<LineRenderer>();
12     }
13 
14     // Use this for initialization
15     void Start()
16     {
17         reader.SetVertexCount(2);//设置顶点
18         reader.SetWidth(0.1f, 0.1f); //设置开始和结束的宽
19         reader.SetColors(Color.red, Color.yellow); //设置开始和结束的颜色
20         reader.SetPosition(0, transform.position);//设置开始坐标
21     }
22 
23     // Update is called once per frame
24     void Update()
25     {
26         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
27         if (Physics.Raycast(ray, out hit)) //当射线碰到物体
28         {
29             reader.SetPosition(1, hit.point);
30         }
31         else
32         {
33             //以下效果相同,
34             //reader.SetPosition(1, ray.GetPoint(1000));
35             reader.SetPosition(1, ray.direction * 1000 - transform.position);
36         }
37     }
38 }
原文地址:https://www.cnblogs.com/nsky/p/4628338.html