我正在一个手机游戏中工作,该手机游戏在同一场景中具有多个区域。每个区域都有一个触发器,当玩家进入其中时,会生成几个对象来拾取它们。我该如何取消激活拾取的物体,以便当玩家再次进入该区域时,该物体不会再次产生?
这是我的代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PathologicalGames;
public class InOutZone_ZONAS: MonoBehaviour {
//Objetos
[Header("Objetos")]
public List<GameObject> spawnPositions;
public List<GameObject> spawnObjects;
private GameObject[] despawnObjects;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
SpawnObjectsZ ();
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
GameObject[] despawnObjects = GameObject.FindGameObjectsWithTag("ItemZona");
for (int i = 0; i < despawnObjects.Length; i++)
{
PoolManager.Pools ["Objetos"].Despawn (despawnObjects[i].transform);
Debug.Log("Despawnea Objetos");
}
}
}
void SpawnObjectsZ()
{
foreach (GameObject spawnPosition in spawnPositions) {
int selection = Random.Range (0, spawnObjects.Count);
PoolManager.Pools ["Objetos"].Spawn (spawnObjects [selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
}
}
}
我该如何取消激活拾取的物体,以便当玩家再次进入该区域时,该物体不会再次产生?
有很多方法可以做到这一点。您可以使用以下命令List
在生成后简单地从中删除GameObjectspawnObjects.Remove(spawnObjects[selection]);
public class Playervitals : MonoBehaviour
{
//Objetos
[Header("Objetos")]
public List<GameObject> spawnPositions;
public List<GameObject> spawnObjects;
private GameObject[] despawnObjects;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
SpawnObjectsZ();
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
GameObject[] despawnObjects = GameObject.FindGameObjectsWithTag("ItemZona");
for (int i = 0; i < despawnObjects.Length; i++)
{
PoolManager.Pools["Objetos"].Despawn(despawnObjects[i].transform);
Debug.Log("Despawnea Objetos");
}
}
}
void SpawnObjectsZ()
{
for (int i = 0; i < spawnPositions.Count; i++)
{
GameObject spawnPosition = spawnPositions[i];
int selection = Random.Range(0, spawnObjects.Count);
PoolManager.Pools["Objetos"].Spawn(spawnObjects[selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
spawnObjects.Remove(spawnObjects[selection]);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句