我對C#很新。我在Unity中創建了一些內容來幫助我更好地學習C#和Unity。C#和Unity3D按鍵被按下時
我想知道爲什麼:
Input.GetKeyDown(KeyCode.UpArrow))
只有觸發一次時,放置在:
void Update()
由於更新是一個循環,爲什麼當我按住按鍵是它不會觸發(以我的情況導致球體移動)?
我已經設法通過使用兩個布爾鍵,當按鍵被釋放時被改變。
這裏是我的,我有玩到移動領域,並模擬加速/減速完整的腳本:
using UnityEngine;
using System.Collections;
public class sphereDriver : MonoBehaviour {
int x ;
bool upPressed = false ;
bool downPressed = false ;
void Start()
{
x = 0 ;
}
void Update()
{
if(x > 0) {
x -= 1 ;
}
if(x < 0) {
x += 1 ;
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
upPressed = true ;
}
else if(Input.GetKeyUp(KeyCode.UpArrow))
{
upPressed = false ;
}
if(upPressed == true)
{
x += 5 ;
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
downPressed = true ;
}
else if(Input.GetKeyUp(KeyCode.DownArrow))
{
downPressed = false ;
}
if(downPressed == true)
{
x -= 5 ;
}
transform.Translate(x * Time.deltaTime/10, 0, 0) ;
}
}
謝謝!由於這兩個函數是Unity特有的,那麼我最初做的就是以最簡單的方式實現C#中我想要的功能嗎? – imperium2335 2012-07-07 13:45:30
是的,這將是一個常用的方法。 – redtuna 2012-08-09 03:10:37