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

末鹿安然 提交于 2020-08-11 05:59:12

一.效果展示

最近在做一款黑暗风的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 visible
	public float lightDuration = 0.025f;

	[Header("Delay Between Flashes")]
	//Delay between lightning flashes
	public float minFlashDelay = 0.05f;
	public float maxFlashDelay = 2.0f;

	[Header("Total Delay")]
	//Time between ligthing effect
	//15 seconds default
	public float minDelay = 5.0f;
	public float maxDelay = 15.0f;

	//Total delay time
	float delay;
	//Delay time between lightning one
	//and lightning two
	float flashDelay;

	bool isWaiting = false;

	[Header("Background Color")]
	//Default background color
	public Color mainBackgroundColor;
	//Lightning background color
	public Color lightningBackgroundColor;

	[Header("Lightning Size")]
	//Minimum size of the lightning sprite
	public float minSize;
	//Maximum size of the lightning sprite
	public float maxSize;

	[Header("Components")]
	//Gun camera
	public Camera gunCamera;
	//The light component
	public Light lightObject;
	//Audio source
	public AudioSource lightningSound;

	//Array holding the lightning sprites
	public Sprite[] lightningSprites;
	//Sprite renderer
	public SpriteRenderer lightningSpriteRenderer;
    public AudioClip thunder;

	//Position and scale values for 
	//the lightning sprite renderer
	float x;
	float y;
    float z;
	Vector3 lightningPos;
	float lightningScale;

	private void Start () {
		//Make sure its off at start
		lightObject.enabled = false;

		//Set the background color of the gun camera
		gunCamera.backgroundColor = mainBackgroundColor;
	}

	private void Update () {
		//Random value for how long the waiting should be
		delay = (Random.Range (minDelay, maxDelay));
		//Random value for how long the delay between the flashes should be
		flashDelay = (Random.Range (minFlashDelay, maxFlashDelay));
		//If is waiting is false, and the random value is 15
		if (!isWaiting) {
			//Start light flash one
			StartCoroutine (LightFlashOne ());
			//Is waiting
			isWaiting = true;
		}
	}

	//First light flash
	private IEnumerator LightFlashOne () {
		//Enable the light
		lightObject.enabled = true;
		//Set a random intensity value
		lightObject.intensity = (Random.Range (minIntensity, maxIntensity));
		//Wait for set amount of time

		//Set the background color of the gun camera
		gunCamera.backgroundColor = lightningBackgroundColor;

		//Enable the lightning sprite renderer
		lightningSpriteRenderer.enabled = true;
		//Show a random lightning sprite from the array
		lightningSpriteRenderer.sprite = lightningSprites 
			[Random.Range (0, lightningSprites.Length)];

		//Get random position for lightning sprite renderer
		x = Random.Range(-150, 150);
		y = Random.Range(55, 65);
        z = Random.Range(300, 450);
		lightningPos = new Vector3 (x, y, z);
		//Choose random scale value
		lightningScale = Random.Range(minSize, maxSize);

		//Move the sprite renderer to the new position
		lightningSpriteRenderer.transform.position = lightningPos;
        lightningSpriteRenderer.transform.localEulerAngles = new Vector3(0, Random.Range(0,360), 0);
		//Set the sprite renderer to the new scale
		lightningSpriteRenderer.transform.localScale = new Vector3 
			(lightningScale,lightningScale,lightningScale);

		yield return new WaitForSeconds (lightDuration);
		//Disable the light
		lightObject.enabled = false;

		//Set the background color of the gun camera
		gunCamera.backgroundColor = mainBackgroundColor;

		//Disable the lightning sprite renderer
		lightningSpriteRenderer.enabled = false;

		//Start the flash delay
		StartCoroutine (FlashDelay ());
	}

	//Delay between LightFlashOne one and LightFlashTwo
	private IEnumerator FlashDelay () {
		//Wait for set amount of time
		yield return new WaitForSeconds (flashDelay);
		//Start light flash two
		StartCoroutine (LightFlashTwo ());
	}

	//Second light flash
	private IEnumerator LightFlashTwo () {
		//Enable the light
		lightObject.enabled = true;
		//Set a random intensity value
		lightObject.intensity = (Random.Range (minIntensity, maxIntensity));

		//Set the background color of the gun camera
		gunCamera.backgroundColor = lightningBackgroundColor;

		//Enable the lightning sprite renderer
		lightningSpriteRenderer.enabled = true;
		//Show a random lightning sprite from the array
		lightningSpriteRenderer.sprite = lightningSprites 
			[Random.Range (0, lightningSprites.Length)];

        //Get random position for lightning sprite renderer
        x = Random.Range(-150, 150);
        y = Random.Range(55, 65);
        z = Random.Range(300, 450);
        lightningPos = new Vector3(x, y, z);
        //Choose random scale value
        lightningScale = Random.Range(minSize, maxSize);

		//Move the sprite renderer to the new position
		lightningSpriteRenderer.transform.position = lightningPos;
        lightningSpriteRenderer.transform.localEulerAngles = new Vector3(0, Random.Range(0, 360), 0);
        //Set the sprite renderer to the new scale
        lightningSpriteRenderer.transform.localScale = new Vector3 
			(lightningScale,lightningScale,lightningScale);

        //Play sound
        lightningSound.clip = thunder;
		lightningSound.Play();

		//Wait for set amount of time
		yield return new WaitForSeconds (lightDuration);
		//Disable the light
		lightObject.enabled = false;

		//Set the background color of the gun camera
		gunCamera.backgroundColor = mainBackgroundColor;

		//Disable the lightning sprite renderer
		lightningSpriteRenderer.enabled = false;

		//Start the waiting timer
		StartCoroutine(Timer());
	}

	//Time between lightnings
	private IEnumerator Timer () {
		//Wait for set amount of time
		yield return new WaitForSeconds (delay);
		//Is not waiting
		isWaiting = false;
	}
}

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

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

在这里插入图片描述

END

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!