Unity 制作雷电特效

记录一下,方便使用

第一种是用LineRenderer

创建两个空物体,把两个空物体分开,一个设置成起点,一个终点  如下图  把起点终点放到脚本里面

 

 

 挂脚本的物体得有LineRenderer组件,不然会报错(下面上代码)

复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[ExecuteInEditMode] //加了这个之后可以在Editor运行
public class Thunder_Lightning : MonoBehaviour
{
    public float detail = 1;//增加后,线条数量会减少,每个线条会更长。  

    public float displacement = 15;//位移量,也就是线条数值方向偏移的最大值  

    public Transform EndPostion;//链接目标  

    public Transform StartPosition;

    public float yOffset = 0;

    private LineRenderer _lineRender;

    private List<Vector3> _linePosList;

    private void Awake()

    {
        _lineRender = GetComponent<LineRenderer>();

        _linePosList = new List<Vector3>();

    }

    private void Update()

    {
      
        if (Time.timeScale != 0)

        {

            _linePosList.Clear();

            Vector3 startPos = Vector3.zero;

            Vector3 endPos = Vector3.zero;

            if (EndPostion != null)

            {

                endPos = EndPostion.position + Vector3.up * yOffset;

            }

            if (StartPosition != null)

            {

                startPos = StartPosition.position + Vector3.up * yOffset;

            }

            //获得开始点与结束点之间的随机生成点

            CollectLinPos(startPos, endPos, displacement);

            _linePosList.Add(endPos);

            //把点集合赋给LineRenderer

            _lineRender.SetVertexCount(_linePosList.Count);

            for (int i = 0, n = _linePosList.Count; i < n; i++)

            {

                _lineRender.SetPosition(i, _linePosList[i]);

            }

        }

    }

    //收集顶点,中点分形法插值抖动  

    private void CollectLinPos(Vector3 startPos, Vector3 destPos, float displace)

    {

        //递归结束的条件

        if (displace < detail)

        {

            _linePosList.Add(startPos);

        }

        else

        {

            float midX = (startPos.x + destPos.x) / 2;

            float midY = (startPos.y + destPos.y) / 2;

            float midZ = (startPos.z + destPos.z) / 2;

            midX += (float)(UnityEngine.Random.value - 0.5) * displace;

            midY += (float)(UnityEngine.Random.value - 0.5) * displace;

            midZ += (float)(UnityEngine.Random.value - 0.5) * displace;

            Vector3 midPos = new Vector3(midX, midY, midZ);

            //递归获得点

            CollectLinPos(startPos, midPos, displace / 2);

            CollectLinPos(midPos, destPos, displace / 2);

        }

    }
}
复制代码

创建材质球给LineRenderer 组件  材质球的图片  和材质球的设置

           

 

 

最后效果是这样的 

 第二种 是粒子特效修改的 下面贴上粒子特效的参数,材质球还用上面的就行

 

 

 

 

 

不喜勿喷

 end 

 

posted @   剑起苍穹  阅读(1666)  评论(2编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
/*鼠标点击特效*/
点击右上角即可分享
微信分享提示