Unity按键未注册

时间:2014-10-11 17:52:52

标签: c# unity3d

在开始之前我应该​​提到我是一个团结的菜鸟。

所以我一直在关注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

3 个答案:

答案 0 :(得分:0)

你说按箭头时它不动。根据您的关键代码分配,您应该按WS

答案 1 :(得分: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));
}
}
}

答案 2 :(得分:0)

真正的解决方案,这样您就不会在 Unity Editor 中犯错误。

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));
}