首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在序列中检测按键?

如何在序列中检测按键?
EN

Game Development用户
提问于 2014-10-06 09:16:11
回答 1查看 581关注 0票数 -1
代码语言:javascript
运行
复制
using UnityEngine;
using System.Collections;

public class TrollingRowlandMK2 : MonoBehaviour {

    int cheat_progress = 0;
    float cheat_delay = 0.0f;

void detect() {if(cheat_progress == 0 && Input.GetKeyDown("1")){
            ++cheat_progress; cheat_delay = 1.0f; Debug.Log ("1 pressed");
    } else if(cheat_progress == 1 && Input.GetKeyDown("2")){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 2 && Input.GetKeyDown("3")){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 3 && Input.GetKeyDown("4")){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 4 && Input.GetKeyDown("5")){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 5 && Input.GetKeyDown("6")){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 6 && Input.GetKeyDown("7")){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 7 && Input.GetKeyDown("8")){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 8 && Input.GetKeyDown("9")){
        ++cheat_progress; cheat_delay = 1.0f;
    } else if(cheat_progress == 9 && Input.GetKeyDown("0")){
        cheat_progress = 0;
        //Some cheats here
        Application.LoadLevel(3);

    }
    /*if(cheat_delay > 0.0f){
        cheat_delay -= Time.deltaTime;
        if(cheat_delay <= 0.0f){
            cheat_delay = 0.0f;
                cheat_progress = 0;}*/
        }
    }

我们试图检测按键并改变级别,就像欺骗代码一样,但是即使我们调试时,它似乎也没有检测到按键。

EN

回答 1

Game Development用户

发布于 2014-10-06 09:52:42

链接Input.KeyDown()并使用进度计数器是一种非常严格的方法。您将发现很难修改或扩展。

您可能会发现Input.inputString的适应性更强。在每个帧上,它将保存自上次帧以来按下的任意键的ascii值。它可以这样使用:

代码语言:javascript
运行
复制
public string cheatCode = "1234567890";
public float timeoutDuration = 1.0f;

private string userInput = "";
private float timeoutTime = 0.0f;

public void Update() {
    if(Input.inputString.Length > 0) {
        timeoutTime = Time.time + timeoutDuration;
        userInput += Input.inputString;
        if(userInput.Contains(cheatCode)) {
            //activate cheat
        }
    }
    else if((Time.time > timeoutTime) && (userInput.Length > 0)) {
        userInput = "";
    }
}
票数 2
EN
页面原文内容由Game Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://gamedev.stackexchange.com/questions/84480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档