2014-10-11 108 views
0

在我開始之前,我應該提到我是一個統一的noob。Unity鍵盤沒有註冊

所以我一直在遵循pong教程,但是我有一個問題。按箭頭時,球拍不會移動。如果你知道如何解決這個問題請讓我知道

我的代碼:

using UnityEngine; 
using System.Collections; 

public class MoveRacket : MonoBehaviour { 
// up and down keys (to be set in the Inspector) 
public KeyCode up; 
public KeyCode down; 

void FixedUpdate() { 
    // up key pressed? 
    if (Input.GetKey(up)) { 
     transform.Translate(new Vector2(0.0f, 0.1f)); 
    } 

    // down key pressed? 
    if (Input.GetKey(down)) { 
     transform.Translate(new Vector2(0.0f, -0.1f)); 
    } 
} 
} 

和控制採摘選項: What shows up when I add the script in the inspector

+0

你真的從組合框中分配嗎? – 2014-10-11 18:00:11

+0

重複問題#http://stackoverflow.com/questions/26318390/key-input-wont-work-in-unity/26318448?noredirect = 1#comment41302388_26318448 不要問問題兩次......如果你不能找到你的答案,然後等待幾個小時,然後再試一次或在聊天中提問:http://chat.stackoverflow.com/rooms/37624/unity3d-developers – 2014-10-11 19:40:31

回答

0

你說,當你按下方向不會移動。根據您的關鍵代碼分配,您應該按WS

+0

我試着改變它,但它仍然沒有工作:( – user1627724 2014-10-11 18:26:21

+0

不,我有兩個球拍,一個是用ws控制的,另一個是用箭頭控制的 – user1627724 2014-10-11 19:10:21

+0

你確定你將劇本附加到場景中的物體上嗎?我想不出發生這種情況的另一個原因。爲我工作 – devil0150 2014-10-11 19:16:46

0
just change the name of your method FxedUpdate() to Update() 

using UnityEngine; 
using System.Collections; 

public class MoveRacket : MonoBehaviour { 
// up and down keys (to be set in the Inspector) 
public KeyCode up; 
public KeyCode down; 

void Update() { 
// up key pressed? 
if (Input.GetKey(up)) { 
    transform.Translate(new Vector2(0.0f, 0.1f)); 
} 

// down key pressed? 
if (Input.GetKey(down)) { 
    transform.Translate(new Vector2(0.0f, -0.1f)); 
} 
} 
} 
+0

爲什麼OP應該這樣做?對於輸入'FixedUpdate()'和'Update()'來說,變化不大。 – FunctionR 2015-01-15 06:56:14

0

真正的解決辦法,這樣你就不會在該統一編輯錯誤。

void FixUodate() 
{ 
    if(Input.GetKey(KeyCode.UpArrow)) 
     transform.Translate(new Vector2(0.0f, 0.1f)); 


    else if (Input.GetKey(KeyCode.DownArrow)) 
     transform.Translate(new Vector2(0.0f, -0.1f)); 
}