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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值