I tried to implement object pooling for my asteroid mockup, but the bullet won’t move anymore, just stays at where it is activated.
Pooling script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Preload : MonoBehaviour
{
public static Preload SharedInstance;
public List<GameObject> preloadobj;
public GameObject objtoload;
public int preloadamount;
public float bspeed = 10f;
public PBullet_Pis ppisbullet;
private Rigidbody2D brb;
private void Awake()
{
SharedInstance = this;
}
// Start is called before the first frame update
void Start()
{
preloadobj = new List<GameObject>();
GameObject tmp;
for (int i = 0; i < preloadamount; i++)
{
tmp = Instantiate(objtoload);
tmp.SetActive(false);
preloadobj.Add(tmp);
}
brb = ppisbullet.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
public GameObject GetPreloadObject()
{
for (int i = 0; i < preloadamount; i++)
{
if (!preloadobj[i].activeInHierarchy)
{
return preloadobj[i];
}
}
return null;
}
public void Shooting(Vector2 direc) //Me desperately trying to bring moving func from bullet script
{
brb.AddForce(direc * this.bspeed); //add a force to bullet
//Destroy(this.gameObject , this.time); //destroy when time up
}
}
Bullet Script:
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEditor.Tilemaps;
using UnityEngine;
public class PBullet_Pis : MonoBehaviour
{
private Rigidbody2D rb; //bullet physics refer
private Vector2 bulletdirec;
public GameObject bspawn;
public float bspeed = 10.0f; //bullet speed
public float time = 10.0f; //time for bullet to despawn
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
//get components
}
private void FixedUpdate()
{
if (gameObject.activeInHierarchy)
{
rb.velocity = this.transform.forward * bspeed;
// rb.AddForce(bspawnup.transform. * bspeed);
if (time> 0)
{
time -= Time.deltaTime;
}
else
{
gameObject.SetActive(false);
time = 10;
}
}
}
public void Shooting() //shooting function, direc variable is refered from player:this.bspawn.up
{
//rb.AddForce(direc * this.bspeed); //add a force to bullet
//Destroy(this.gameObject , this.time); //destroy when time up
}
}
Shooting function from player:
void Shoot()
{
rigbod.velocity = new Vector2(rot.x , rot.y).normalized * thrust; //apply a new velocity with the negative direction and the thrust, find out the effects of normalized.
GameObject bullet = Preload.SharedInstance.GetPreloadObject();
if (bullet != null )
{
bullet.transform.position = bspawn.transform.position;
bullet.transform.rotation = bspawn.transform.rotation;
bullet.SetActive(true);
// brb.AddForce(transform.forward * preload.Bspeed());
}
// PBullet_Pis bullet = Instantiate(this.bulletpf, this.bspawn.position, this.bspawn.rotation); //instance bullet
I tried calling transform from pooled object, doesn’t work. Tried calling rigidbody at pooling script from bullet and move the bullet there, doesn’t work. All other tutorial I found only let bullet travel in a fixed direction, instead on where the player is aiming…
Thanks in advance!!