2013年9月8日 星期日

Unity3D 教學 ﹥22 Unity 教學

http://www.cg.com.tw/Unity/htm/Unity_022.asp

我們將要探討遊戲開發時最重要的兩個元素 - Collision Detection 碰撞偵測、Ray Casting 光跡傳遞。
為了偵測遊戲物件之間的物理性互動,最常用的方法是使用 Collider 碰撞器元件,就是一個圍繞在物件周圍看不到的網,
在三度空間的兩個物件之間,畫一條看不到的向量直線
Ray casting 也可以 retrieve 許多其他有用的資訊,例如直線的距離、。

doorOpenTime
doorOpenSound
doorCloseSound

開門的腳本

Animations
Disabling automatic animation
將屬性編輯器的 Play Automatically(自動播放)取消勾選
Element 0 ---> idle 閒置 ( Do nothing state )
Element 1 ---> dooropen 開門
Element 2 ---> doorchut 關門
開發遊戲時,我們需要決定在那個物件加上腳本,若是一個物件與多個物件將可能產生相互作用,那麼為這個物件撰寫單一的腳本,比起為其多個物件分別撰寫腳本,通常是第一種方式比較有效率。 With this in mind, when writing scripts for a game such as this, we will write a script to be applied to the player character in order to check for collisions with many objects in our environment, rather than a script made for each object the player may interact with, whick checks for the player.
doorIsOpen 檢查目前門是否為打開的狀態。
doorTimer 設定開門後自動關閉的時間,隱蔽的浮點變數。
currentDoor
doorOpenTime
doorOpenSound
doorCloseSound

新增一個腳本,命名為「PlayerCollisions」
private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0;
private var currentDoor : GameObject;
var doorOpenTime : float = 3.0
var doorOpenSound : AudioClip;
var doorCloseSound : AudioClip;
function OnControllerColliderHit(hit : ControllerColliderHit){
}
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.tag == "
}
function OpenDoor(){
audio.PlayOneShot(doorOpenSound);
}


播放動畫的 JavaScript 語法
語法:遊戲物件名稱.animation.Play("動畫片段名稱");
範例:HouseAll.animation.Play("Anim_OpenDoor");
播放音效的 JavaScript 語法
語法:audio.PlayOneShot(AudioClip)
範例:audio.PlayOneShot(OpenDoorSound);

Audio source 元件附加到遊戲物件。
audio.Play() 播放音訊
audio.PlayOneShot() 播放一次音訊(例如開門或關門的音效只需要播放一次,而不是重複播放)
audio.Stop() 停止音訊
audio.Pause() 暫停音訊



使用字型
需要 Assets > Import New Asset
匯入 TTF ( TrueType font ) 或 OTF ( OpenType font ) 檔案到遊戲專案的 Assets 資料夾。