Unity中C#模拟打雷效果——Light控制

C#模拟打雷效果

最近看到有些插件打雷效果很逼真,就想着自己试着弄一个,感觉效果还可以
大家可以试试看
如下:
将脚本挂载在一个点光源上就可以使用了,具体效果可以 自己试着调整(注:点光源要足够大,至少要盖到需要看到的地方)
using UnityEngine;
using System.Collections;

public class Thundering : MonoBehaviour
{
//public Transform ground;

//float groundWidth, groundlength;

Light thunderLight;
public Color colorLight;

[Header("变强光停留最大时间")]
public float maxThunderDur = 0.5f;
float thunderDuration;
[Header("变弱光停留最大时间")]
public float maxThunderBreakDur = 0.5f;
float thunderBreakDuration;
[Header("下一循环停留最大时间")]
public float maxThunderRestDur = 0.5f;
float thunderRestDur;

int serialThunderTime;
[Header("闪光次数")]
public int maxSerialThunderTime = 5;


[Header("最大强度范围")]
public float IntensityMaxOne = 6f;
public float IntensityMaxTwo = 10f;
float Max;
[Header("最小强度范围")]
public float IntensityMinOne = 1.5f;
public float IntensityMinTwo = 3f;
float Min;

void Awake()
{
    thunderLight = GetComponent<Light>();
    thunderLight.color = colorLight;
}

// Use this for initialization
void Start()
{
    StartCoroutine(Thunder());
}
void FixedUpdate()
{
    thunderLight.color = colorLight;
}
IEnumerator Thunder()
{
    while (true)
    {
        
        serialThunderTime = Random.Range(0, maxSerialThunderTime + 1);
        for (int i = 0; i < serialThunderTime; i++)
        {
            thunderDuration = Random.Range(0, maxThunderDur);
            Max = Random.Range(IntensityMaxOne, IntensityMaxTwo);
            thunderLight.intensity = Max;
            yield return new WaitForSeconds(thunderDuration);
            Min = Random.Range(IntensityMinOne, IntensityMinTwo);
            thunderLight.intensity = Min; 
            thunderBreakDuration = Random.Range(0, maxThunderBreakDur);
            yield return new WaitForSeconds(thunderBreakDuration);
        }

        thunderRestDur = Random.Range(0, maxThunderRestDur);
        yield return new WaitForSeconds(maxThunderRestDur);
    }
}

}

欢迎留言评论······
一个学习的人,关注一下······

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值