- Home /
Help spawning pooled objects with moving spawn point?
I am making a side scrolling endless space shooter where my backgrounds are set up at different depths and my camera and player are set at start to go 7.5f and the first background replaces the last background after the camera goes so far and with the layers i can make a cool parallax effect all of that works fine but i am having issues spawning in my enemys i have an empty gameobject attached to my player and i have that object ahead of me offscreen so that i can use that for my spawn position x and then I fill in the y value with a random.range with the min screen height and the max screen height and everything works... well for the first pool spawn but everytime they are respawned after that they keep spawning further and further ahead of the spawn point to where they are too far from the screen to ever let me catch up so they just keep activating and deactivating any help would be greatly appreciated this has been driving me crazy all night i just cant seem to figure out whats going on
Answer by SkyriftStudios · Nov 13, 2019 at 12:17 AM
Is your pooled objects connected to your parrallax or any other objects because if your player or your object parent is changing size then your pool objects will too. Make sure that the parent is set to either null or an object that has no changes in transform, position, rotation, scale, etc. You can make a script to make the parent follow the player without directly being attached.
Can you provide images of your unity editor and the problem? Maybe a snippet of code for your pooling object system? Just so I can have a better understanding of this problem.
I fixed the spawning issue by taking the velocity off of the drone but now the drones come across the screen too fast and any way that I try to add velocity to them it seems to grow exponentially on every respawn any ideas on how to set my velocity just once ?
Answer by ShadowsFallStudios · Nov 13, 2019 at 12:28 AM
I have only been learning to code for 2 months so be easy on me lol This is my first project where I am doing everything on my own. I think the issue may be involved with the drone ai script where i set the speed to 5f and i think it might be adding to that speed everytime it reactivates thank you for your reply and any help you can give.
OBJECT POOLER Script
public class ObjectPooler : MonoBehaviour { [System.Serializable] public class Pool { public string tag; public GameObject prefab; public int size; }
public static ObjectPooler Instance;
private void Awake()
{
Instance = this;
}
public List<Pool> pools;
public Dictionary<string, Queue<GameObject>> PoolDictionary;
private float randomY;
private void Start()
{
PoolDictionary = new Dictionary<string, Queue<GameObject>>();
foreach(Pool pool in pools)
{
Queue<GameObject> objectPool = new Queue<GameObject>();
for (int i = 0; i < pool.size; i++)
{
GameObject obj = Instantiate(pool.prefab);
obj.SetActive(false);
objectPool.Enqueue(obj);
}
PoolDictionary.Add(pool.tag, objectPool);
}
}
private void Update()
{
randomY = Random.Range(-7f, 11f);
}
public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation)
{
position = new Vector3(transform.position.x, randomY, transform.position.z);
rotation = Quaternion.Euler(0f, -90f, 0f);
if (!PoolDictionary.ContainsKey(tag))
{
Debug.LogWarning("Pool with tag " + tag + " doesn't exist.");
return null;
}
GameObject objectToSpawn = PoolDictionary[tag].Dequeue();
objectToSpawn.SetActive(true);
objectToSpawn.transform.position = position;
objectToSpawn.transform.rotation = rotation;
IPooledObject pooledObj = objectToSpawn.GetComponent<IPooledObject>();
if(pooledObj != null)
{
pooledObj.OnObjectSpawn();
}
PoolDictionary[tag].Enqueue(objectToSpawn);
return objectToSpawn;
}
interface script
public interface IPooledObject { void OnObjectSpawn(); }
droneSpawner Script
public class DroneSpawner : MonoBehaviour { private Vector3 spawnLocation; private float randomY; private ObjectPooler objectPooler; private bool isSpawning;
private void Start()
{
objectPooler = ObjectPooler.Instance;
}
private void Update()
{
Debug.Log(spawnLocation);
spawnLocation = new Vector3(transform.position.x, randomY, transform.position.z);
randomY = UnityEngine.Random.Range(-7f, 11f);
StartCoroutine(SpawnDronesCo());
}
private IEnumerator SpawnDronesCo()
{
if (!isSpawning)
{
isSpawning = true;
yield return new WaitForSeconds(UnityEngine.Random.Range(2f, 6f));
objectPooler.SpawnFromPool("Drone", spawnLocation, Quaternion.Euler(0f, -90f, 0f));
isSpawning = false;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612214724im_/https://answers.unity.com/themes/thub/images/avi.jpg)