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
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中
到此,可以執行一下感受詭異的氣氛。。。