(一)AlarmLight游戏警报灯闪烁渐变效果的设
警报灯的闪烁由Light的Intensity来设置光亮的强度变化,设置三个参数 highIntensity=0.8f , lowIntensity=0.1f和targetIntensity来控制变化
if (isAlarmOn)
{
light.enabled = true;
if (Mathf.Abs(light.intensity-targetIntensity) < 0.05f)//更换targetIntensity的值
{
if (targetIntensity == highIntensity)
{
targetIntensity = lowIntensity;
}
else
{
targetIntensity = highIntensity;
}
}
light.intensity= Mathf.Lerp(light.intensity, targetIntensity, Time.deltaTime * animationSpeed);
}
else
{
light.enabled = false;
//消除警报
light.intensity = Mathf.Lerp(light.intensity, 0, Time.deltaTime * animationSpeed);
}
(二)GameController
1.控制背景声音的切换:正常行动时normal音乐 ,触发警报后panic音乐,使用vector3.Lerp渐变
AlarmLight._intance.isAlarmOn = this.isAlarmOn;
//控制警报器声音的开关
if (isAlarmOn)
{
musicNormal.volume = Mathf.Lerp(musicNormal.volume, 0, Time.deltaTime *musicFadeSpeed);
musicPanic.volume= Mathf.Lerp(musicPanic.volume, 0.8f, Time.deltaTime * musicFadeSpeed);
PlaySiren();
}
else
{
musicNormal.volume = Mathf.Lerp(musicNormal.volume, 0.5f, Time.deltaTime * musicFadeSpeed);
musicPanic.volume = Mathf.Lerp(musicPanic.volume, 0, Time.deltaTime * musicFadeSpeed);
StopSiren();
}
2.控制 警报器的触发设置SeePlayer():被敌人看见 触碰到红色的激光装置
警报器的播放声音PlaySiren()
public void SeePlayer(Transform player)
{
isAlarmOn = true;
lastPlayerPostion = player.position;
}
private void PlaySiren()
{
foreach(GameObject go in sirens)
{
AudioSource audio = go.GetComponent<AudioSource>();
if (!audio.isPlaying)
{
audio.Play();
audio.volume = 0.4f;
}
}
}
private void StopSiren()
{
foreach (GameObject go in sirens)
{
AudioSource audio = go.GetComponent<AudioSource>();
audio.Stop();
}
}
(三)实时摄像机的trriger检测
这里为实时摄像机的添加了旋转的动画,更改 animation Rotation.y即可
public void OnTriggerStay(Collider col)
{
if (col.tag == Tags.player)
{
GameController._instance.SeePlayer(col.gameObject.transform);
}
}
(四)激光装置的闪烁和触发警报(同上)
闪烁效果通过控制该prefab的MeshRenderer即可实现
if (isFlicker)
{
if (renderer.enabled)
{
timer += Time.deltaTime;
if (timer >= onTime)
{
renderer.enabled = false;
collider.enabled = false;
timer = 0;
}
}
if (!renderer.enabled)
{
timer += Time.deltaTime;
if (timer >= offTime)
{
renderer.enabled = true;
collider.enabled = true;
timer = 0;
}
}
}
(五)自动门的监测以及声音的播放
设置参数count判断门应该close还是open
private void OnTriggerEnter(Collider col)
{
if (requireKey)
{
if (col.tag == Tags.player)
{
PlayerMove player = col.GetComponent<PlayerMove>();
if (player.hasKey)
{
count++;
}
}
}
else
{
if (col.tag == Tags.player )
{
count++;
}
else if(col.tag == Tags.enemy&& col.isTrigger == false)
{
count++;
}
}
}
离开 trigger count--,同上
声音的播放和动画的播放
void Update()
{
anim.SetBool("Close", count <= 0);
//播放声音
if(anim.IsInTransition(0))//在base layer层级是否发生动画的转换
{
if (!audio.isPlaying)
{
audio.Play();
}
}
}
(六)