unity 检测同时按下两个按键
时间: 2024-10-01 17:09:48 浏览: 126
在Unity中检测玩家同时按下两个键,可以使用Input系统提供的功能。你可以创建一个脚本,利用`Input.GetKeyDown()`函数检查特定键是否被按下,并使用位操作符(如`|`)来组合多个按键检查。以下是一个简单的示例:
```csharp
using UnityEngine;
using System;
public class KeyPressDetector : MonoBehaviour
{
public KeyCode key1, key2; // 你要检测的两个键
private bool isKey1Pressed = false;
private bool isKey2Pressed = false;
void Update()
{
if (Input.GetKeyDown(key1))
{
isKey1Pressed = true;
}
else if (Input.GetKeyUp(key1)) // 当键释放时恢复为false
{
isKey1Pressed = false;
}
if (Input.GetKeyDown(key2))
{
isKey2Pressed = true;
}
else if (Input.GetKeyUp(key2))
{
isKey2Pressed = false;
}
// 检查两个键是否都被按下
if (isKey1Pressed && isKey2Pressed)
{
Debug.Log("Both keys " + key1.ToString() + " and " + key2.ToString() + " are pressed simultaneously.");
}
}
}
```
在这个例子中,当玩家同时按下`key1`和`key2`时,控制台会打印一条消息。
阅读全文
相关推荐


















