Top of the day to you all!
I’ve run into a frustrating end today. The problem here is that I’ve been using a simple code to shoot bullet in a fan fashion way with this code:
private void Update()
{
if (Time.time > nextShot)
{
GetSpeed();
nextShot = Time.time + fireRate;
Quaternion qAngle = Quaternion.AngleAxis(numShots / 2 * spreadAngle, transform.up) * transform.rotation;
Quaternion qDelta = Quaternion.AngleAxis(spreadAngle, transform.up);
for (int i = 0; i < numShots; i++)
{
shot = Instantiate(shot, transform.position, qAngle);
qAngle = qDelta * qAngle;
}
}
}
Much much later I’ve built myself an Object Pool. I’ve changed all my instantiating code to set.Active. It works perfectly with everything else but with this code. This is the updated code for it to work with my Object Pool:
void Update()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
string name = shot.name;
GameObject obj = ObjectPoolerScript.current.GetPooledObject(name);
if (obj == null) return;
Quaternion qAngle = Quaternion.AngleAxis(-numShots / 2 * spreadAngle, transform.up) * transform.rotation;
Quaternion qDelta = Quaternion.AngleAxis(spreadAngle, transform.up);
for (int i = 0; i < numShots; i++)
{
obj.transform.position = transform.position;
obj.transform.rotation = qAngle;
obj.SetActive(true);
qAngle = qDelta * qAngle;
}
The thing is; shots don’t spread anymore and the command doesn’t activate more than one bullet at a time (always at a specific location depending on the spreadAngle value).
I’m missing something here so if anyone of you have a clue about that let me know!
@FortisVenaliter
Here is the object Pool script that I use:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class ObjectPoolItem
{
public GameObject objectToPool;
public string poolName;
//public string objectName;
public int pooledAmount;
public bool willGrow = true;
}
public class ObjectPoolerScript : MonoBehaviour
{
public const string DefaultRootObjectPoolName = "Pooled Objects";
public static ObjectPoolerScript current;
public string rootPoolName = DefaultRootObjectPoolName;
public List<GameObject> pooledObjects;
public List<ObjectPoolItem> itemsToPool;
void Awake()
{
current = this;
}
private void Start ()
{
if (string.IsNullOrEmpty(rootPoolName))
rootPoolName = DefaultRootObjectPoolName;
GetParentPoolObject(rootPoolName);
pooledObjects = new List<GameObject>();
foreach (var item in itemsToPool)
{
for (int i = 0; i < item.pooledAmount; i++)
{
CreatePooledObject(item);
}
}
}
private GameObject GetParentPoolObject(string objectPoolName)
{
// Use the root object pool name if no name was specified
if (string.IsNullOrEmpty(objectPoolName))
objectPoolName = rootPoolName;
GameObject parentObject = GameObject.Find(objectPoolName);
//Create the parent object
if(parentObject == null)
{
parentObject = new GameObject();
parentObject.name = objectPoolName;
//Add sub pools to the root object po ol
if (objectPoolName != rootPoolName)
parentObject.transform.parent = GameObject.Find(rootPoolName).transform;
}
return parentObject;
}
public GameObject GetPooledObject(string tag)
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects_.activeInHierarchy && pooledObjects*.name == tag)*_
{
return pooledObjects*;*
}
}
foreach (var item in itemsToPool)
{
if (item.objectToPool.name == tag)
{
if (item.willGrow)
{
return CreatePooledObject(item);
}
}
}
return null;
}
private GameObject CreatePooledObject (ObjectPoolItem item)
{
GameObject obj = Instantiate(item.objectToPool);
//Get parent for this pooled object and assign the new object to it
var parentPoolObject = GetParentPoolObject(item.poolName);
obj.transform.parent = parentPoolObject.transform;
obj.name = item.objectToPool.name;
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}
}
Have a good day!!