C# 如何在unity中获得两个相同的输入?
我正在尝试创建一个Android游戏,但现在正在使用输入键进行测试。 我想做的是两次得到“正确”的钥匙,但不知道如何得到。 这是我的密码:C# 如何在unity中获得两个相同的输入?,c#,unity3d,C#,Unity3d,我正在尝试创建一个Android游戏,但现在正在使用输入键进行测试。 我想做的是两次得到“正确”的钥匙,但不知道如何得到。 这是我的密码: void Update () { if(Input.GetKeyDown("left")) { Debug.Log("in the left zone"); isLeft = true; } if(Input.GetKeyDown("right")) { isRight
void Update () {
if(Input.GetKeyDown("left"))
{
Debug.Log("in the left zone");
isLeft = true;
}
if(Input.GetKeyDown("right"))
{
isRight = true;
if(Input.GetKeyDown("right"))
{
isDoubleRight = true;
isRight = false;
Debug.Log("in the double right zone");
}
}
}
此外,同样的逻辑也适用于未来的触摸。它与按键输入的逻辑是否相同?
有什么建议吗?
提前谢谢我自己解决了,但不确定是否正确。希望这对别人有帮助
if(Input.GetKeyDown("right") && isRight)
{
isDoubleRight = true;
isRight = false;
Debug.Log("in the double right zone");
}
else if(Input.GetKeyDown("right"))
{
isRight = true;
}
首先
你自己的答案检测不到“双重压力”。。。它只是每秒按一次切换一个变量
更正双重按下的代码将是:
using UnityEngine;
using System.Collections;
public class ButtonTest : MonoBehaviour {
float timeOut = 0.25f;
float timeLimit; // the latest time a double-press can be made
string lastButton; // the name of the button last checked
// an example of using "CheckDouble" is in here
void Update (){
if (Input.GetKeyDown("right")) CheckDouble("right");
}
// The function to check for double-presses of a given button
bool CheckDouble (string newButton) {
// if new button press is the same as last, AND recent enough
if(newButton==lastButton && Time.time<timeLimit )
{
timeLimit=Time.time; // expires the double-press
Debug.Log("Double Press");
return true;
}
else
{
timeLimit = Time.time+timeOut; // set new Limit for double-press
lastButton = newButton; // for future checks
Debug.Log("Single Press (so far)");
return false;
}
}
}
使用UnityEngine;
使用系统集合;
公共类按钮测试:单一行为{
浮动超时=0.25f;
float timeLimit;//可以进行双击的最新时间
string lastButton;//上次选中的按钮的名称
//这里有一个使用“CheckDouble”的例子
无效更新(){
if(Input.GetKeyDown(“right”))选中double(“right”);
}
//检查给定按钮的两次按下的功能
bool CheckDouble(字符串新按钮){
//如果新按钮按下与上一次相同,且最近足够
如果(newButton==lastButton&&Time.Time),我同意这种方法。但我建议也添加一个关于时间延迟的测试(实际上,它可以在两个压力之间经过很长时间,但在这种情况下,系统也会识别双压力)。