Hi,
I'm trying to make a 3D infinite runner.
For spawn enemies i'm trying to use the object pooling
I would like to disable the enemies when the player position is greater on the z axis, finally respawn enemies ahead.
(the enemies are stationary obstacles)
I tried this but it does not work:
using UnityEngine;
using System.Collections;
public class DestroyEnemy : MonoBehaviour {
public GameObject player;
void Update()
{
if (player.transform.position.z > gameObject.transform.position.z) {
gameObject.SetActive (false);
}
}
}
This is the code for the object pooling
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyGenPool : MonoBehaviour {
public float spawnTime = 1f;
public GameObject enemy;
public int pooledAmount = 20;
List enemies;
private Vector3 distanceBetween = new Vector3(0f,0f,500f);
void Start () {
enemies = new List ();
for (int i = 0; i < pooledAmount; i++)
{
GameObject obj = (GameObject)Instantiate(enemy);
obj.SetActive (false);
enemies.Add(obj);
}
InvokeRepeating ("spawnEnemy", spawnTime, spawnTime);
}
void spawnEnemy () {
for (int i = 0; i < enemies.Count; i++) {
if (!enemies [i].activeInHierarchy)
{
enemies [i].transform.position += distanceBetween;
enemies [i].transform.rotation = transform.rotation;
enemies [i].SetActive (true);
break;
}
}
}
}
How can i fix? Thanks in advance.
↧