- Home /
Question by
wjordan · Jun 09, 2014 at 03:57 PM ·
rotationobject poolfire rateroation
Adding Rotation & Fire Rate to an Object Pool
I have a turret that fires bullets. I know how to rotate the bullet so it fires in the direction the turret is pointing and I know how to manipulate the fire rate.
The problem was that my game started to slow down, heavily. So I went back some steps and added an Object Pool. I just don't understand how to re-incorporate those items.
I'm still a noob, so I try to get by with manuals and tutorials. Any help would be greatly appreciated.
Here is my Object Pool:
using UnityEngine;
using System.Collections;
public class ObjectPool : MonoBehaviour {
GameObject[] projectiles = null;
public int numberOfProjectilesToCreate = 0;
public GameObject ProjectilePrefab;
// Use this for initialization
void Start () {
projectiles = new GameObject[numberOfProjectilesToCreate];
InstantiateProjectiles();
}
// Update is called once per frame
void Update () {
ActivateProjectile();
}
private void InstantiateProjectiles(){
for (int i = 0; i < numberOfProjectilesToCreate; i++) {
projectiles[i] = Instantiate(Resources.Load ("Prefab/ProjectilePrefab")) as GameObject;
projectiles[i].SetActive(false);
}
}
private void ActivateProjectile(){
for (int i = 0; i < numberOfProjectilesToCreate; i++) {
if(projectiles[i].activeSelf == false){
projectiles[i].SetActive(true);
projectiles[i].GetComponent<Projectile>().Activate();
return;
}
}
}
}
Here was my rotation:
Vector3 position = new Vector3(transform.position.x, transform.position.y * (transform.localScale.y / 2));
Instantiate(ProjectilePrefab, position, transform.rotation);
}
Here was my fire rate:
InvokeRepeating ("LaunchProjectile", 1, 1);
Also, is it best practice to have Object Pool as its own script? Since I need the projectiles to come from the turret shouldn't that go into my turret script? A little confused. Thanks.
Comment