unity火焰粒子效果

gihub传送门,注意要手动import unity自带的particle System资源(因为文件太多所以没放到github)
https://github.com/ddghost/unity3d_n/tree/%E7%B2%92%E5%AD%90%E6%95%88%E6%9E%9C

这次作业要做的是粒子效果,我就来做一个火焰的效果吧。
下面就是成果(emmm。。。感觉还是不是太像),还是有点火的样子的嘛,点击按钮可以调节火的大小,左右摇摆。
这里写图片描述

在百度随便搜了张图,参考着来调整火焰(尽管最后也不是很像)
这里写图片描述

首先新建一个unity项目,导入unity自带的particle System资源,里面也有不少粒子效果,但这次只是想用一下里面的贴图而已(略略路.jpg),然后就可以开干了。
这里写图片描述

试过几个贴图,感觉还是白烟这张贴图好,因为本身白色,颜色调了比较明显,在Renderer处的Material和Trail选择ParticleSmokeWhite这个贴图。
这里写图片描述

然后再调整相关的属性,duration设为2,startLiftime设为2,start speed设为4,start size设为5。
这里写图片描述

由于火焰应该是由一个比较小的口发出,因此在shape这个地方将angle设为0,radius设为0.3。
这里写图片描述

考虑到火焰是底部小,中部大,上部小的,因此设置size over Lifetime,调整它的变化曲线,在曲线上右键就可以加一个节点,加一个节点,大概调整一下,调整的时候可以看着粒子的变化自己选择。
这里写图片描述

然后完成以上步骤就可以看到这个火的效果的形状已经有了。
这里写图片描述

有了形状,还要有颜色,修改color over lifetime,因为由之前的样例图可以看到火一般底部是蓝火,因此把开始的颜色设为蓝色,然后中部设为黄色,同时在上面调整透明度的地方,把最后的透明度调为0,这样火就会越来越透明了,看起来较为平缓。
这里写图片描述

修改完颜色的效果如下
这里写图片描述

完成了这些,一个粗略的火效果已经做完了,然后就是代码部分,代码较为简单,主要就是调整火的左右摇摆和大小而已,代码如下,直接把它挂到粒子系统上即可。

public class UserGui : MonoBehaviour {
    ParticleSystem particleSystem;
    ParticleSystem.ForceOverLifetimeModule forceMode;
    // Use this for initialization
    void Start () {
        particleSystem = GetComponent<ParticleSystem>();
        forceMode = particleSystem.forceOverLifetime;
    }

    // Update is called once per frame
    void Update () {

    }

    void OnGUI(){
        if( GUI.Button(new Rect(10 , 30 , 50 , 30) ,  "left") ){
            ParticleSystem.MinMaxCurve temp = forceMode.x ;
            temp.constantMax = temp.constantMax - 0.5f;
            forceMode.x = temp;
        }

        if( GUI.Button(new Rect(10 , 70 , 50 , 30) ,  "right") ){
            ParticleSystem.MinMaxCurve temp = forceMode.x ;
            temp.constantMax = temp.constantMax + 0.5f;
            forceMode.x = temp;
        }

        if( GUI.Button(new Rect(10 , 110 , 50 , 30) ,  "big") ){
            particleSystem.startSize =  particleSystem.startSize * 1.11f;
            particleSystem.startLifetime = particleSystem.startLifetime * 1.11f;
        }

        if( GUI.Button(new Rect(10 , 150 , 50 , 30) ,  "small") ){
            particleSystem.startSize =  particleSystem.startSize * 0.9f;
            particleSystem.startLifetime = particleSystem.startLifetime * 0.9f;
        }
    }

}

最后,值得注意的是,由于代码中用到了forceOverLifetime,因此在粒子系统上一定要把这一项勾了,否则就无法左右摇摆了,在这里也是坑了很久。

猜你喜欢

转载自blog.csdn.net/ddghsot/article/details/80488036