天天看点

HW8 粒子系统与流动效果

本次作业基本要求是三选一

1、简单粒子制作

  • 按参考资源要求,制作一个粒子系统,参考资源
  • 使用 3.3 节介绍,用代码控制使之在不同场景下效果不一样

2、完善官方的“汽车尾气”模拟

  • 使用官方资源资源 Vehicle 的 car, 使用 Smoke 粒子系统模拟启动发动、运行、故障等场景效果

3、参考 http://i-remember.fr/en 这类网站,使用粒子流编程控制制作一些效果, 如“粒子光环”

  • 可参考以前作业

我选择了第3个 ,制作粒子光环,步骤如下:

  1. 在场景中新建一个空对象,命名为ParticleHalo;
  2. 在ParticleHalo下新建一个Particle System子对象:添加组件->Effects->Particle System,命名为Halo;
HW8 粒子系统与流动效果
  1. 新建C#脚本,命名为ParticleData.cs:

    该脚本提供了一个保存每个粒子数据的结构,分别对应其极坐标角度,半径长,在半径方向上游离速度,运动方向(顺时针/逆时针),代码如下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ParticleData : MonoBehaviour
    {
    	public float angle = 0f;
    	public float radius = 0f;
    	public float speed = 0f;
    	public bool clockwise = false;
    
    	public ParticleData(float _angle,float _radius, float _speed, bool _clockwise)
    	{
    		angle = _angle;
    		radius = _radius;
    		speed = _speed;
    		clockwise = _clockwise;
    	}
    }
               
  2. 新建C#脚本,命名为ParticleHalo.cs:

    初始化粒子大小、粒子生命周期和粒子数量;设置粒子系统参数,发射粒子;并将粒子发射到环的不同位置上,代码如下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ParticleHalo : MonoBehaviour
    {
        private ParticleSystem par_sys;
        private ParticleSystem.MainModule main;
        private ParticleSystem.Particle[] halo;
        private ParticleData[] par_data;
    
        public float size = 0.05f;              //粒子大小
        public float time = 9999f;              //粒子生命周期
        public int par_count = 10000;			//粒子数量
    
        // Start is called before the first frame update
        void Start()
        {
            halo = new ParticleSystem.Particle[par_count];
            par_data = new ParticleData[par_count];
    
            //初始化粒子系统
            par_sys = this.GetComponent<ParticleSystem>();
            main = par_sys.main;
            main.loop = false;
            main.startLifetime = time;
            main.startSpeed = 0f;
            main.startSize = size;
            main.maxParticles = par_count;
            par_sys.Emit(par_count);
            par_sys.GetParticles(halo);
    
            randomPlace();
        }
    
        //随机放置粒子
        private void randomPlace()
        {
            for (int i = 0; i < par_count; i++)
            {
                float r = Random.Range(5f, 12f);
                if (r % 3 == 1)
                    r = Random.Range(7f, 10f);
                else if (r % 3 == 2)
                    r = Random.Range(8f, 9f);
                float angle = Random.Range(0f, 360f);
                float theta = angle / 180 * Mathf.PI;
                float speed = Random.Range(0.5f, 1.5f);
                int flag = Random.Range(0, 2);
                bool clockwise = (flag == 0) ? false : true;
    
    
                halo[i].position = new Vector3(r * Mathf.Cos(theta), r * Mathf.Sin(theta), 0f);
                par_data[i] = new ParticleData(angle, r, speed, clockwise);
            }
            par_sys.SetParticles(halo, halo.Length);
        }
    
    
        // Update is called once per frame
        void Update()
        {
            for (int i = 0; i < par_count; i++)
            {
                if (par_data[i].clockwise)          //顺时针
                    par_data[i].angle -= Random.Range(0.01f, 0.5f);
                else                                //逆时针
                    par_data[i].angle += Random.Range(0.01f, 0.5f);
                par_data[i].angle %= 360f;
                float theta = par_data[i].angle / 180 * Mathf.PI;
    
                //粒子进行游离
                float r = par_data[i].radius;
                r += Mathf.PingPong(Time.time * par_data[i].speed, 2f);
    
                halo[i].position = new Vector3(r * Mathf.Cos(theta), r * Mathf.Sin(theta), 0);
            }
    
            par_sys.SetParticles(halo, halo.Length);
        }
    
    }
    
               
  3. 完成后将脚本ParticleHalo.cs挂载到Halo对象上,点击运行,效果如下:
HW8 粒子系统与流动效果
  1. 可以看到已经初步实现了粒子光环,但效果不够逼真,好像不是每个粒子在独立运动,于是添加一个差分层变量tier,让粒子角度的增量不都一样。这个差分层变量使所有粒子分成了10个阵营,每个阵营角度增量不一样。

    同时还在后面添加了一个系数,这个系数和粒子的半径关联,使得离中心越远的粒子角度增量越小,转得越慢。

    经过这个设置,粒子的运动显得生动了许多(但图片效果看不太出来…😓)

    最终结果

HW8 粒子系统与流动效果

项目地址:

https://gitee.com/liuhz5/game-unity/tree/master/hw8

继续阅读