unity3d - Unity Door open with "e"?
问题描述
I would like to be able to open my Gate with "e". I've got to the point where the player has to stand in front of the Gate and then you have to press "e", but I don't know how to do it. So far I've written that
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player") && Input.GetKeyDown("e"))
{
Debug.Log("door opens");
}
}
}
解决方案
由于OnTriggerEnter
仅称为玩家与门碰撞的单帧,因此请使用OnTriggerStay
. 另一种选择是存储您是否与门发生碰撞,然后检测输入Update
。
如果您使用OnTriggerStay
:
private void OnTriggerStay(Collider other)
{
if(other.CompareTag("Player") && Input.GetKeyDown("e"))
{
Debug.Log("door opens");
}
}
如果您想使用OnTriggerEnter
静止,我也会使用OnTriggerExit
知道它们何时不再与它发生碰撞。
private bool touchingDoor = false;
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
touchingDoor = true;
}
}
private void OnTriggerExit(Collider other)
{
if(other.CompareTag("Player"))
{
touchingDoor = false;
}
}
private void Update()
{
if(Input.GetKeyDown("e") && touchingDoor)
{
Debug.Log("door opens");
}
}
推荐阅读
- c++ - reinterpret_cast vs 严格别名
- c++ - 没有类型转换的模板派生类的 C++ 访问成员
- javafx - 从 AnchorPane 获取可见元素
- php - 在 Laravel 中将 Model 实例与它自己的 Class 相关联的好方法是什么?
- ios - 如何检查我的应用程序是否启用了 Face id?
- apache - 在文件中记录存储在 .htpasswd 中的 htaccess apache 访问用户
- aframe - 按下按钮期间:将相机移动到视线方向
- laravel - Laravel Doctrine, (doctrine:schema:update) 不会为新添加的实体创建表
- javascript - React 中的和解
- javascript - QUnit 测试套件完成时登录到终端?