我创建了一个脚本,当相机的盒子碰撞器与门的刚体碰撞时,玩家可以使用按钮 E 打开门。当我测试它时,门没有关闭,我得到一个错误。 这是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour
{
public GameObject door_closed, door_opened, intDot;
public AudioSource open, close;
public bool opened;
void OnTriggerStay(Collider other)
{
if (other.CompareTag("MainCamera"))
{
if (opened == false)
{
intDot.SetActive(true);
if (Input.GetKeyDown(KeyCode.E))
{
door_closed.SetActive(false);
door_opened.SetActive(true);
intDot.SetActive(false);
//open.Play();
StartCoroutine(repeat());
opened = true;
}
}
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("MainCamera"))
{
intDot.SetActive(false);
}
}
IEnumerator repeat()
{
yield return new WaitForSeconds(4.0f);
opened = false;
door_closed.SetActive(true);
door_opened.SetActive(false);
//close.Play();
}
}
我尝试在开始时激活门,但仍然不起作用。一切对我来说都很完美。