目前,你應該對如何旋轉或移動物件有初步認識,但這樣是不足夠製作遊戲的,一個遊戲還必需接受與玩家的互動,譬喻,當我按下鍵盤的「上鍵」時,它要懂得向前行。
要達成這個目的,我們需要用Input類別,並在update()函數中不斷判斷用戶是否有按下某些鍵。以下列出一些常用的鍵盤事件:
function update(){
// 上下左右
if(Input.GetAxisRaw("Horizontal") > 0) Debug.Log("按下了右鍵");
else if(Input.GetAxisRaw("Horizontal") < 0) Debug.Log("按下了左鍵");
if(Input.GetAxisRaw("Vertical") > 0) Debug.Log("按下了上鍵");
else if(Input.GetAxisRaw("Vertical") < 0) Debug.Log("按下了下鍵");
// 其他鍵盤事件
if(Input.getKey("a")) Debug.Log("按下了a");
}
除了鍵盤事件外,Input類別還可判斷Button事件,滑鼠事件等等,你可以參考以下文件來學習如何進一步使用Input類別來建立你的遊戲:
Input類別:http://unity3d.com/support/documentation/ScriptReference/Input.html
要達成這個目的,我們需要用Input類別,並在update()函數中不斷判斷用戶是否有按下某些鍵。以下列出一些常用的鍵盤事件:
function update(){
// 上下左右
if(Input.GetAxisRaw("Horizontal") > 0) Debug.Log("按下了右鍵");
else if(Input.GetAxisRaw("Horizontal") < 0) Debug.Log("按下了左鍵");
if(Input.GetAxisRaw("Vertical") > 0) Debug.Log("按下了上鍵");
else if(Input.GetAxisRaw("Vertical") < 0) Debug.Log("按下了下鍵");
// 其他鍵盤事件
if(Input.getKey("a")) Debug.Log("按下了a");
}
除了鍵盤事件外,Input類別還可判斷Button事件,滑鼠事件等等,你可以參考以下文件來學習如何進一步使用Input類別來建立你的遊戲:
Input類別:http://unity3d.com/support/documentation/ScriptReference/Input.html
Life_programmer
沒有留言:
張貼留言