我是靠谱客的博主 感动鲜花,这篇文章主要介绍unity官方demo学习之Stealth(五)游戏控制器,现在分享给大家,希望可以做个参考。

五,游戏控制器  主要:记录主角最后出现的位置
1,创建空对象,命名为gameController,设置tag,gameController,将audio中的music_normal拖入音频剪辑中(这是正常游戏音乐背景),勾选paly
   on awake,loop
2,在gameController下创建空的子对象,命名为secondaryMusic,拖进去music_panic音频,勾选loop,设置volume为0 (玩家被发现时音乐背景)

3,为gameController添加脚本DoneLastPlayerSighting

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using UnityEngine; using System.Collections; public class DoneLastPlayerSighting : MonoBehaviour { public Vector3 position = new Vector3(1000f, 1000f, 1000f); // The last global sighting of the player. public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f); // The default position if the player is not in sight. public float lightHighIntensity = 0.25f; // The directional light's intensity when the alarms are off. public float lightLowIntensity = 0f; // The directional light's intensity when the alarms are on. public float fadeSpeed = 7f; // How fast the light fades between low and high intensity. public float musicFadeSpeed = 1f; // The speed at which the private DoneAlarmLight alarm; // Reference to the AlarmLight script. private Light mainLight; // Reference to the main light. private AudioSource panicAudio; // Reference to the AudioSource of the panic msuic. private AudioSource[] sirens; // Reference to the AudioSources of the megaphones. void Awake () { // Setup the reference to the alarm light. alarm = GameObject.FindGameObjectWithTag(DoneTags.alarm).GetComponent<DoneAlarmLight>(); // Setup the reference to the main directional light in the scene. mainLight = GameObject.FindGameObjectWithTag(DoneTags.mainLight).light; // Setup the reference to the additonal audio source. panicAudio = transform.FindChild("secondaryMusic").audio; // Find an array of the siren gameobjects. GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag(DoneTags.siren); // Set the sirens array to have the same number of elements as there are gameobjects. sirens = new AudioSource[sirenGameObjects.Length]; // For all the sirens allocate the audio source of the gameobjects. for(int i = 0; i < sirens.Length; i++) { sirens[i] = sirenGameObjects[i].audio; } } void Update () { // Switch the alarms and fade the music. SwitchAlarms(); MusicFading(); } void SwitchAlarms () { // Set the alarm light to be on or off. alarm.alarmOn = (position != resetPosition); // Create a new intensity. float newIntensity; // If the position is not the reset position... if(position != resetPosition) // ... then set the new intensity to low. newIntensity = lightLowIntensity; else // Otherwise set the new intensity to high. newIntensity = lightHighIntensity; // Fade the directional light's intensity in or out. mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime); // For all of the sirens... for(int i = 0; i < sirens.Length; i++) { // ... if alarm is triggered and the audio isn't playing, then play the audio. if(position != resetPosition && !sirens[i].isPlaying) sirens[i].Play(); // Otherwise if the alarm isn't triggered, stop the audio. else if(position == resetPosition) sirens[i].Stop(); } } void MusicFading () { // If the alarm is not being triggered... if(position != resetPosition) { // ... fade out the normal music... audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime); // ... and fade in the panic music. panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime); } else { // Otherwise fade in the normal music and fade out the panic music. audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime); panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime); } } }

变量:敌人或摄像机最后发现玩家的位置 (3个1000f是 代表不存在的坐标,AI不知道主角的位置,AI得不到主角的坐标会将值设为这个)
      重置主角坐标点为 默认坐标,即玩家没有被发现
      两个分别为主光线的最大,最小强度
      光线强度变化速率
      背景音乐切换速率


      警报光线脚本引用
      主光灯引用
      恐怖音乐组件引用
      警报喇叭数组


函数:awake(),设置alarm脚本DoneAlarmLight  通过tag获得对象,再获得其组件脚本文件DoneAlarmLight  
      设置主光线  通过tag获得对象,通过快捷方式获得该组件引用
      设置恐怖音乐组件  通过transform.FindChild查找子元素对象,然后再通过快捷方式获得该组件引用
      获得警报喇叭的数组 通过该标签获得该对象存入数组
      创建siren数组,长度与上面数组一致 
      
      依次获取sirenGameObjects中的AudioSource组件存入siren数组中


      Update(),调用切换警报光和音乐切换函数


      SwitchAlarms (),切换警报光函数
      通过玩家当前位置和重置位置的比较设置是否开警报灯
根据当前位置与重置位置的关系(不相等即玩家暴露)设置新的主光的强度,然后平滑的变化到新的光强
对于所有警报灯,若玩家暴露且没开,那就开;玩家没暴露,就关

      MusicFading()
玩家暴露时,背景音乐变0,恐怖音效变大;否则,反之


最后将gameController拖入prefab中
到此,可以运行一下感受诡异的气氛。。。

最后

以上就是感动鲜花最近收集整理的关于unity官方demo学习之Stealth(五)游戏控制器的全部内容,更多相关unity官方demo学习之Stealth(五)游戏控制器内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(91)

评论列表共有 0 条评论

立即
投稿
返回
顶部