unity官方案例Stealth中 激光栅栏 忽明忽暗效果实现
程序员文章站
2022-07-02 14:49:55
...
LaserBlinking脚本挂在激光栅栏上
public class LaserBlinking : MonoBehaviour {
public float OnTime;//激光开多久
public float OffTime;//灭多久
private float time;//计时,判断time>ontime或offtime:是否到了开启或者关闭激光的时间
private Light laserLight;//激光栅栏的Light组件,要获得它,以控制照射栅栏的灯光的亮度
private Renderer laserRenderer;//MeshRenderer组件,同上,以控制栅栏的渲染(出现)或不渲染(消失)
// Use this for initialization
void Start () {
laserLight = GetComponent<Light>();
laserRenderer = GetComponent<Renderer>();
}
// Update is called once per frame
void Update () {
time += Time.deltaTime;//计时增加
if (laserRenderer.enabled &&time > OnTime )//MeshRenderer组件为启用状态,表示激光开启的时长超过了规定开启时长
{
SwitchBeam();//关闭激光
}
if (!laserRenderer.enabled && time > OffTime)//MeshRenderer组件为禁用状态,表示激光关闭的时长超过了规定关闭时长
{
SwitchBeam();//开启
}
}
//控制激光栅栏状态的转变,开启:渲染组件和灯光组件启用;关闭:两者禁用
void SwitchBeam()
{
time = 0;//每一次开启和关闭,计时器归0
laserRenderer.enabled = !laserRenderer.enabled;
laserLight.enabled = !laserLight.enabled;
}
}