作者kagami91582 (GaryJ)
看板GameDesign
標題[請益] unity長按的設定
時間Wed Sep 23 13:01:27 2015
在之前詢問了Update()的細節後 自己有去改了一下
不過還是有一些問題
主要設定的程式是這樣子
void Update(){
for (int i = 0; i < NumStrings; ++i) {
ButtonsJustPressed [i] = false;
CheckKeyCode (StringKeys [i], i);
}
}
void CheckKeyCode( KeyCode code, int stringIndex ){
if( Input.GetKeyDown( code ) )
{
OnStringChange( stringIndex, true );
}
if( Input.GetKey( code ) && !ButtonsPressed[ stringIndex ] ){
OnStringChange( stringIndex, true );
}
if( Input.GetKeyUp( code ) ){
OnStringChange( stringIndex, false );
}
}
若是在物品落下時 利用長按的方法去接住 要怎麼改比較好?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.216.73
※ 文章網址: https://webptt.com/m.aspx?n=bbs/GameDesign/M.1442984491.A.6CD.html
1F:推 cjcat2266: 按下flag = true,放開flag = false 09/23 14:26
2F:→ cjcat2266: if (flag && 物件碰到) 接住(); 09/23 14:26
3F:→ laikyo: 你要不要看一下Input.GetKey官方描述... 09/23 17:09
4F:→ kagami91582: 其實看了很久還是看不懂(淚 09/23 23:34
5F:→ tomnelson: docs.unity3d.com/ScriptReference/Input.GetKey.html 09/24 01:05
6F:→ tomnelson: Input.GetKey不就是當按鍵一直被按住沒放開時會return 09/24 01:07
7F:→ tomnelson: true嗎? 你應該是 GetKeyDown/GetKey/GetKeyUp 三個狀 09/24 01:09
8F:→ tomnelson: 態處理那邊沒搞好,往那方向去思考一下吧! 09/24 01:10
9F:→ tomnelson: 我的想法是GetKeyDown(x)後設flag(x)=true,當GetKey(x) 09/24 01:12
10F:→ tomnelson: 為true且flag(x)=true維持一段時間,就去做長按的事,而 09/24 01:14
11F:→ tomnelson: GetKeyUp(x)=true且flag(x)=true時,將flag(x)設成false 09/24 01:15
12F:→ tomnelson: 至於"維持一段時間"那部份,可能你要試驗看看,或許用 09/24 01:16
13F:→ tomnelson: counter(x)=counter(x)+1的方式就可以,未必要用到時間 09/24 01:20
14F:→ tomnelson: docs.unity3d.com/ScriptReference/Time.html 09/24 01:20
15F:→ tomnelson: 裡頭的 deltaTime (Time.deltaTime) 之類的,總之,你就 09/24 01:22
16F:→ tomnelson: 動手&再動腦試試囉~ 09/24 01:22
17F:→ kagami91582: 感謝提點 之後我是這樣子改啦 09/24 01:50
19F:→ kagami91582: 按下一個key然後不放開這樣 的確有接到物品了 09/24 01:51
20F:→ kagami91582: 雖然目的是要讓他同時跑兩個啦~ 但已經先從無線迴圈 09/24 01:52
21F:→ kagami91582: 跳出來了 再次感謝(淚 09/24 01:53
22F:→ kagami91582: 不然已經鬼打牆一段時間 自己英文又無敵廢(炸 09/24 01:54
23F:推 tomnelson: 有從 loop 裡出來就好~ 恭喜! 09/24 02:43