Unity 粒子特效之 烟火式拖尾效果的简单实现
1、打开Unity,新建一个工程,在场景中添加一个 Particle System,具体如下图


2、调整 Particle System 的 Shape 参数为 Sphere 半径设置为 0.01,并且添加重力,具体如下图


3、添加 Particle System 的 Size Over Lifetime设置为平滑的从大变到小,并设置 Color Over Lifetime 渐变颜色,具体参数可以按照自己需要设置,具体效果图如下


4、在调整 Particle System 的 Emission 参数和其他基本参数,具体如下,运行场景并拖动Particle System,效果具体如下图


5、在 Particle System 添加 Noise, 其中 Strength 为 3, Frequency 为 1,具体根据需要合理调整即可,运行场景并拖动Particle System,效果具体如下图


6、给 Particle System 添加 Sphere 父物体,并且 Sphere 添加 Rigidbody,并把他们作为预制体,具体如下图

7、在工程中新建一个脚本 Fire,双击脚本打开进行代码编辑,具体如下图


8、Fire 脚本具体内容如下:
using UnityEngine;
public class Fire : MonoBehaviour
{
//添加预制体参数
public GameObject cannonBall;
public float forceSpeed = 300.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//当按下空格键生成并发射物体
if (Input.GetKeyDown(KeyCode.Space))
{
//生成物体并给他添加力
GameObject go = Instantiate(cannonBall, transform.position, transform.rotation);
go.GetComponent<Rigidbody>().AddForce(go.transform.forward * forceSpeed);
}
}
}
9、脚本编译正确,回到Unity,在场景中添加一个 Plane,和 GameObject,并且调整 前方向与水平面有一定的向上角度,并把脚本挂载到 GameObject 上,预制体赋值,具体如下图


10、运行场景,按下 Space 键,模拟发射子弹,具体效果如下图
