简体   繁体   English

Unity按键未注册

[英]Unity keypress not registering

Before I start I should mention I am a unity noob. 在开始之前,我应该提到我是一个团结的菜鸟。

So I have been following a pong tutorial,but I have a problem. 所以我一直在关注乒乓球教程,但是我有一个问题。 The rackets are not moving when you press the arrow. 当您按下箭头时,球拍不会移动。 If you know how to fix this please let me know 如果您知道如何解决此问题,请告诉我

My code: 我的代码:

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

and the control picking option: 和控件选择选项: 当我在检查器中添加脚本时会显示什么

You said it doesn't move when you press the arrow. 您说按箭头时它不会移动。 According to your key code assignments, you should press W or S . 根据您的键码分配,您应按WS

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

The real solution so that you don't make mistakes in the Unity Editor . 真正的解决方案,这样您就不会在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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM