如何使一个对象出现和消失在不同的时间在Unity播放模式?



我想在Unity3D中制作一个对象,在某些时间播放模式下随机出现和消失几次。物体发生的时间有待确定。下面的代码显示一个随机的立方体,并在3秒后消失。但它不应该在开始时可见,然后出现几次。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger: MonoBehaviour
{
public GameObject Object;
void Start()
{
StartCoroutine(ShowAndHide());
}

IEnumerator ShowAndHide()
{
Object.SetActive(true);
yield return new WaitForSeconds(3);
Object.SetActive(false);
}
}

您需要为此创建一个circle类型的循环。在协程中使用这段代码。

bool active = true; //if you want a stop this circle then false active value..
while(active)
{
Object.SetActive(!Object.activeSelf); // or activeInHierarchy
yield return new WaitForSeconds("yourTime"); 
} 

我现在意识到随机lol:)

yield return new WaitForSeconds(Random.Range(0f,'yourMaxValue'));

最新更新