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 }
实现line renderer写字效果的方法通常是将字体转换为线条的形式,并使用Line Renderer组件在屏幕上渲染出来。以下是一个基本的实现示例: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class WriteText : MonoBehaviour { public Font font; public Material material; public float lineWidth = 0.1f; public int fontSize = 32; public string text = "Hello World"; private LineRenderer lineRenderer; void Start() { lineRenderer = gameObject.AddComponent<LineRenderer>(); lineRenderer.material = material; lineRenderer.useWorldSpace = false; CharacterInfo charInfo; font.RequestCharactersInTexture(text, fontSize, FontStyle.Normal); Vector3[] positions = new Vector3[text.Length * 2]; int[] indices = new int[(text.Length - 1) * 2]; for (int i = 0; i < text.Length; i++) { font.GetCharacterInfo(text[i], out charInfo, fontSize); positions[i * 2] = new Vector3(charInfo.minX, 0, 0); positions[i * 2 + 1] = new Vector3(charInfo.maxX, 0, 0); if (i > 0) { indices[(i - 1) * 2] = i * 2 - 1; indices[(i - 1) * 2 + 1] = i * 2; } } lineRenderer.positionCount = positions.Length; lineRenderer.SetPositions(positions); lineRenderer.startWidth = lineWidth; lineRenderer.endWidth = lineWidth; lineRenderer.numCapVertices = 5; lineRenderer.numCornerVertices = 5; lineRenderer.SetIndices(indices, MeshTopology.Lines, 0); } } ``` 在这个示例中,我们首先在Start函数中创建了一个Line Renderer组件并将其附加到该GameObject上。然后,我们使用Font类中的RequestCharactersInTexture函数将字体中的字符加载到纹理中。接下来,我们计算每个字符的位置,并将其作为一个线条的两个顶点添加到positions数组中。最后,我们将indices数组设置为适当的线段索引,以便Line Renderer可以正确地绘制线条。 请注意,此示例中的代码仅用于演示目的。如果您要实现更复杂的文本渲染效果,例如文字曲线或3D字体,您需要使用更高级的技术和算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值