2014-10-11 109 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)); 
}