让我以一种新手的方式开始说,所以请保持简单,哈哈
我正试着在Unity中建立一个传送物体的入口。我找不到它,所以我查阅了一个教程,找到了这段代码。它工作得很好,除了一个小问题。通过门户的对象(我刚才使用的是球体)将在门户之间不断来回移动,本质上会陷入循环中。有没有办法在函数上设置一个缓冲区或“冷却时间”,这样它就不会立即再次触发?我将此代码放在两个门户上,并将“spawnPoint”设置为另一个门户。所以橙色门户的spawnPoint是蓝色门户,反之亦然。这是代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Portal : MonoBehaviour
{
public Transform spawnPoint;
// Update is called once per frame
void OnTriggerEnter(Collider other)
{
other.gameObject.transform.position = spawnPoint.position;
}
}如果我说得不够清楚,请告诉我。
发布于 2021-05-09 22:00:57
许多选项之一是使用协程https://docs.unity3d.com/Manual/Coroutines.html
就像这样。
bool canBeUsed = true;
void OnTriggerEnter(Collider other)
{
if(canBeUsed)
{
canBeUsed = false;
StartCoroutine(Example());
other.gameObject.transform.position = spawnPoint.position;
}
}
IEnumerator Example()
{
yield return new WaitForSeconds(5);
canBeUsed = true;
}多个门户的一个变量。
选项1:
创建空对象并重命名它。也许就叫它"PortalManager“吧。然后像下面这样创建新脚本,并将其添加到我们的PortalManager对象中。https://prnt.sc/12rlzny
using UnityEngine;
public class PortalsManager : MonoBehaviour
{
public static bool CanBeUsed = true;
}然后,门户脚本将如下所示:
使用System.Collections;使用UnityEngine;
public class test : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (PortalsManager.CanBeUsed)
{
PortalsManager.CanBeUsed = false;
StartCoroutine(Example());
Debug.Log("AAAA");
// other.gameObject.transform.position = spawnPoint.position;
}
}
IEnumerator Example()
{
yield return new WaitForSeconds(5);
PortalsManager.CanBeUsed = true;
}
}选项2:
不创建新对象。只需像这样创建脚本:
public static class PortalsManager
{
public static bool CanBeUsed = true;
}门户中的代码与选项1中的代码完全相同
所有这些我都进行了现场测试:)
https://stackoverflow.com/questions/67458507
复制相似问题