1. 程式人生 > >unity官方demo學習之Stealth(五)遊戲控制器

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

相關推薦

unity官方demo學習Stealth遊戲控制器

五,遊戲控制器  主要:記錄主角最後出現的位置 1,建立空物件,命名為gameController,設定tag,gameController,將audio中的music_normal拖入音訊剪輯中(這是正常遊戲音樂背景),勾選paly    on awake,loop 2,

unity官方demo學習Stealth角色動畫控制器

1,(建立玩家靜止狀態)在Animator資料夾中 右鍵,create->Animator Controller,建立一個動畫控制器,命名為PlayerAnimator,建立引數,float:Speed;bool:Snesking;bool:Dead:bool:Sho

unity官方demo學習Stealth單開門動畫

1,將modles中的door_generic_slide拖入層級檢視,位置:-6,0,7;角度:90.子物件勾選 use light probes使光可以照到門 2,父物件新增Sphere collider使門可以檢測到敵人或玩家的靠近,center:y:1,radius

unity官方demo學習Stealth二十四敵人AI

1,新增指令碼檔案DoneEnemyAI using UnityEngine; using System.Collections; public class DoneEnemyAI : MonoBehaviour { public float patrolSpeed

unity官方demo學習Stealth二十敵人視聽範圍

為敵人新增指令碼DoneEnemySight using UnityEngine; using System.Collections; public class DoneEnemySight : MonoBehaviour { public float fieldO

unity官方demo學習Stealth十一角色移動

十一,角色移動 為char_ethan新增指令碼DonePlayerMovement using UnityEngine; using System.Collections; public class DonePlayerMovement : MonoBehaviour

Hibernate學習

ring int 表示 gen prop generator 需要 blog hibernate 簡述 多對多關系映射 多對多關系映射需要一張中間表來維護關系      一:Role類與Function類 1 publi

PHP學習

設有 src fault ... 根據 條件判斷 滿足 循環 獲取 2017.08.13 Day 5  周日  晴 PHP-順序結構 順序結構就像一條直線,按著順序一直往下執行。我們編寫的代碼默認都是按照順序結構執行的。 PHP條件結構之if…else… 條件結

MySQL學習MySQL高級查詢

code left 功能 限定查詢 外鏈接 spa size 平均數 asc MySQL統計函數   count():統計數量;   max():統計最大值;   min():統計最小值;   avg():統計平均數;   sum():統計和; Select

Hadoop學習Hadoop集群搭建模式和各模式問題

數據 場景 模式 問題 沒有 問題: 重裝 故障 style 分布式集群的通用問題 當前的HDFS和YARN都是一主多從的分布式架構,主從節點---管理者和工作者 問題:如果主節點或是管理者宕機了。會出現什麽問題? 群龍無首,整個集群不可用。所以在一主多從的架構中都會

Python學習爬蟲正則表示式爬去名言網

auth Python標準庫 我們 color 匯總 eight code 比較 school 爬蟲的四個主要步驟 明確目標 (要知道你準備在哪個範圍或者網站去搜索) 爬 (將所有的網站的內容全部爬下來) 取 (去掉對我們沒用處的數據) 處理數據(按照我們想要的

Hive學習DbVisualizer配置連接hive

ado lan inf files AD sha comm HR 下載地址 一、安裝DbVisualizer 下載地址http://www.dbvis.com/ 也可以從網上下載破解版程序,此處使用的版本是DbVisualizer 9.1.1 具體的安裝步驟可以百度,

C++再學習

我們 實參 對象傳遞 color virtual 轉換 版本 部分 尊重 1.繼承和動態綁定在兩個方面簡化了我們的程序   能夠容易地定義與其他類相似但又不相同的新類,能夠更容易地編寫忽略這些相似類型之間區別的程序  P471 2.之所以稱通過繼承而相關聯的類型為多態類型,

JAVA基礎學習數組的定義

對象 ava void 數據類型 class 語法 info int .com 什麽是數組:就是一堆相同類型的數據放一堆(一組相關變量的集合) 定義語法: 聲明並開辟數組     數據類型 數組名[] = new 數據類型[長度]; 分布完成 聲明數組:數據類型

學習淺談:三種語句結構,vim編輯器快捷鍵及使用方法,find命令使用

vim編輯器循環;forwhileuntil for 變量 in 列表; do 循環體 done e.g for I in ‘seq 1 $FILE‘ ; doecho "Hello,‘head -n $I

Druid學習Druid的資料攝取任務型別

作者:Syn良子 出處:https://www.cnblogs.com/cssdongl/p/9885534.html 轉載請註明出處 Druid的資料攝取任務型別 Druid支援很多種型別的資料攝取任務.任務通過CURL POST的方式提交到Overlord節點然後分配給middle manager

webpack學習

熱模組替換 熱模組替換(HMR)是webpack提供的最有用的功能之一,它讓各種模組可以在執行時更新而無需重新整理,本篇主要注重於實現。 ps:HMR是為開發模式設計的,也只能用於開發模式。 啟用HRM 啟用HRM只需要更新webpack-dev-server的配置,然後使用webpack的內建外掛,

機器學習

吳恩達教授的機器學習課程的第五週相關內容: 1、代價函式 首先引入一些便於稍後討論的新標記方法: 假設神經網路的訓練樣本有 m 個,每個包含一組輸入 x 和一組輸出訊號 y, L 表示神經網路層數, S I 表示每層的 neuron 個數(SL 表示輸出層神經元個數), S L 代表

Kafka學習Kafka在zookeeper中的存儲

序號 hadoop state 空閑 pre 離開 substr doc 退出 當kafka啟動的時候,就會向zookeeper裏面註冊一些信息,這些數據也稱為Kafka的元數據信息。 一、Kafka在zookeeper中存儲結構圖 二、分析 根目錄下的結構 服務端開啟的

PKI學習-----------------------SSL雙向認證日誌分析

根據上一篇的執行結果,我們根據它的日誌分析互動的過程 "C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2018.1.5\lib\idea_rt.jar=52038:D:\IntelliJ