2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 【Unity】天气特效:打雷下雨

【Unity】天气特效:打雷下雨

时间:2021-01-16 16:52:34

相关推荐

【Unity】天气特效:打雷下雨

文章目录

一.效果展示二.下雨三.打雷

一.效果展示

最近在做一款黑暗风的FPS小游戏,看到资源包的demo scene里有雷雨效果,就学了下并用到了自己的游戏里。下面是先效果展示:

//移动端gif可以点开来循环播放,但pc端要再看一遍只能刷新…

二.下雨

首先找一张雨点的贴图,做成材质球

shader用legacy shader/particles/additive,选贴图,把颜色改成自己想要的效果(如果选particles/alpha blended会很暗,像我效果图里一样黑色的雨)

创建一个粒子系统,命名为rain,把rotation的x 从-90改成90,粒子就会往下运动

把renderer里的material改成自己新建的材质,粒子就会变成雨点

将shape改成box,就能形成一个类似降雪的效果。根据自己需求,调节scale大小,形成一个降雨区。

勾选color over lifetime,创建一个从透明到不透明的渐变效果

但现在还只是类似下雪的效果,我们需要调节他的速度和密度

duration 和 start lifetime 来调节循环周期和粒子存活时间

start speed和 gravity modifier(重力增强) 调节速度

max particles 调节粒子的密度(模拟雨量大小)

rate over time 同时影响了密度和速度

雨滴大小可以修改start size,点最右边的小黑点可以选择范围

如果你觉得看不到雨点效果,试着把start color改成白色(最明显),color over time先关掉,然后renderer的参数改成这样再看看

看到了效果再继续调各种参数,直到满足你想要的的效果

三.打雷

准备工作,闪电贴图如下(放进unity里用sprite editor切开)

创建一个空对象,命名为lighting,添加一个sprite renderer组件

改变相机背景色(闪电划过时要变亮一点)

把clear flag设置为solid color,即可自己选用背景色

然后是控制脚本的编写

大体思路是 每次updata随机生成delay(两次打雷间隔)和flashdelay(电光和雷声的间隔)

iswaiting,判断是否开启协程。

Flashone中把电光light启用,光强度随机,闪电sprit也启用,形状和位置大小都随机,

相机背景色也改成稍亮的背景

Yield return new waitforsecond(lightduration)

相机背景和闪电和光关闭,再开启一个flashdelay的协程,在该协程里开启flashtwo(带声音的第二次电闪)

Two和one类似,但加上了声音(模拟先光后声,简单点也可以只有声音没有新的电光)

然后开启新协程,把iswaiting改回来

using UnityEngine;using System.Collections;public class LightningScript : MonoBehaviour {[Header("Light Intensity")]public float minIntensity = 1.0f;public float maxIntensity = 3.0f;[Header("Light Duration")]//How long the light should be visiblepublic float lightDuration = 0.025f;[Header("Delay Between Flashes")]//Delay between lightning flashespublic float minFlashDelay = 0.05f;public float maxFlashDelay = 2.0f;[Header("Total Delay")]//Time between ligthing effect//15 seconds defaultpublic float minDelay = 5.0f;public float maxDelay = 15.0f;//Total delay timefloat delay;//Delay time between lightning one//and lightning twofloat flashDelay;bool isWaiting = false;[Header("Background Color")]//Default background colorpublic Color mainBackgroundColor;//Lightning background colorpublic Color lightningBackgroundColor;[Header("Lightning Size")]//Minimum size of the lightning spritepublic float minSize;//Maximum size of the lightning spritepublic float maxSize;[Header("Components")]//Gun camerapublic Camera gunCamera;//The light componentpublic Light lightObject;//Audio sourcepublic AudioSource lightningSound;//Array holding the lightning spritespublic Sprite[] lightningSprites;//Sprite rendererpublic SpriteRenderer lightningSpriteRenderer;public AudioClip thunder;//Position and scale values for //the lightning sprite rendererfloat x;float y;float z;Vector3 lightningPos;float lightningScale;private void Start () {//Make sure its off at startlightObject.enabled = false;//Set the background color of the gun cameragunCamera.backgroundColor = mainBackgroundColor;}private void Update () {//Random value for how long the waiting should bedelay = (Random.Range (minDelay, maxDelay));//Random value for how long the delay between the flashes should beflashDelay = (Random.Range (minFlashDelay, maxFlashDelay));//If is waiting is false, and the random value is 15if (!isWaiting) {//Start light flash oneStartCoroutine (LightFlashOne ());//Is waitingisWaiting = true;}}//First light flashprivate IEnumerator LightFlashOne () {//Enable the lightlightObject.enabled = true;//Set a random intensity valuelightObject.intensity = (Random.Range (minIntensity, maxIntensity));//Wait for set amount of time//Set the background color of the gun cameragunCamera.backgroundColor = lightningBackgroundColor;//Enable the lightning sprite rendererlightningSpriteRenderer.enabled = true;//Show a random lightning sprite from the arraylightningSpriteRenderer.sprite = lightningSprites [Random.Range (0, lightningSprites.Length)];//Get random position for lightning sprite rendererx = Random.Range(-150, 150);y = Random.Range(55, 65);z = Random.Range(300, 450);lightningPos = new Vector3 (x, y, z);//Choose random scale valuelightningScale = Random.Range(minSize, maxSize);//Move the sprite renderer to the new positionlightningSpriteRenderer.transform.position = lightningPos;lightningSpriteRenderer.transform.localEulerAngles = new Vector3(0, Random.Range(0,360), 0);//Set the sprite renderer to the new scalelightningSpriteRenderer.transform.localScale = new Vector3 (lightningScale,lightningScale,lightningScale);yield return new WaitForSeconds (lightDuration);//Disable the lightlightObject.enabled = false;//Set the background color of the gun cameragunCamera.backgroundColor = mainBackgroundColor;//Disable the lightning sprite rendererlightningSpriteRenderer.enabled = false;//Start the flash delayStartCoroutine (FlashDelay ());}//Delay between LightFlashOne one and LightFlashTwoprivate IEnumerator FlashDelay () {//Wait for set amount of timeyield return new WaitForSeconds (flashDelay);//Start light flash twoStartCoroutine (LightFlashTwo ());}//Second light flashprivate IEnumerator LightFlashTwo () {//Enable the lightlightObject.enabled = true;//Set a random intensity valuelightObject.intensity = (Random.Range (minIntensity, maxIntensity));//Set the background color of the gun cameragunCamera.backgroundColor = lightningBackgroundColor;//Enable the lightning sprite rendererlightningSpriteRenderer.enabled = true;//Show a random lightning sprite from the arraylightningSpriteRenderer.sprite = lightningSprites [Random.Range (0, lightningSprites.Length)];//Get random position for lightning sprite rendererx = Random.Range(-150, 150);y = Random.Range(55, 65);z = Random.Range(300, 450);lightningPos = new Vector3(x, y, z);//Choose random scale valuelightningScale = Random.Range(minSize, maxSize);//Move the sprite renderer to the new positionlightningSpriteRenderer.transform.position = lightningPos;lightningSpriteRenderer.transform.localEulerAngles = new Vector3(0, Random.Range(0, 360), 0);//Set the sprite renderer to the new scalelightningSpriteRenderer.transform.localScale = new Vector3 (lightningScale,lightningScale,lightningScale);//Play soundlightningSound.clip = thunder;lightningSound.Play();//Wait for set amount of timeyield return new WaitForSeconds (lightDuration);//Disable the lightlightObject.enabled = false;//Set the background color of the gun cameragunCamera.backgroundColor = mainBackgroundColor;//Disable the lightning sprite rendererlightningSpriteRenderer.enabled = false;//Start the waiting timerStartCoroutine(Timer());}//Time between lightningsprivate IEnumerator Timer () {//Wait for set amount of timeyield return new WaitForSeconds (delay);//Is not waitingisWaiting = false;}}

新建一个manger,挂上脚本,调参数,加对象(相机背景色和电闪时的背景色,主相机,电闪时的环境光,雷声,闪电sprite…)

注意别忘记给manger加audio sourece组件,用于播放打雷音效

END

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。