This post has been wikified, any user with enough reputation can edit it.
Bullet not going in the direction which it is facing.
I am working on a 2D game in which the player rotates around its own axis in space, and when the player shoots a bullet, he moves in the opposite direction of the shot bullet. (This is just the player movement part).
Problem: I have created an object pool from which I draw the bullets that I have to shoot, but sometimes the bullets do not go in the direction they are supposed to go. They spawn facing the correct direction and move in some other direction. And if I do not use the object pool, this problem goes away (but I do want to use the object pool).
Here are the codes:
Bullet Code -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour {
public float speed = 5f;
public float mag = 1f;
public GameObject bulletBottom;
public float outbound = 0f;
private Rigidbody2D bullet;
public Rigidbody2D player;
public float playerSpeed = 5f;
private float dirx = 0;
private float diry = 0;
private bool oneTime = false;
private Vector3 bottomLeftScreenPoint;
private Vector3 topRightScreenPoint;
public Vector2 screenBounds;
private ObjectPooler objPooler;
void Start () {
oneTime = false;
objPooler = ObjectPooler.Instance;
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
bottomLeftScreenPoint = Camera.main.ScreenToWorldPoint(new Vector3(0f, 0f, Camera.main.transform.position.z));
topRightScreenPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
bullet = GetComponent<Rigidbody2D>();
}
private void OnEnable()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
oneTime = false;
}
void FixedUpdate () {
if (!oneTime)
RecoilandShoot();
if ( transform.position.y < bottomLeftScreenPoint.y - outbound || transform.position.y > topRightScreenPoint.y + outbound || transform.position.x < bottomLeftScreenPoint.x - outbound || transform.position.x > topRightScreenPoint.x + outbound)
{
//Destroy(gameObject);
bullet.velocity = new Vector2(0, 0);
gameObject.SetActive(false);
objPooler.poolDictionary["Bullet"].Enqueue(gameObject);
}
}
void RecoilandShoot() {
dirx = (transform.position.x - bulletBottom.transform.position.x) * mag;
diry = (transform.position.y - bulletBottom.transform.position.y) * mag;
bullet.velocity = new Vector2(0, 0);
bullet.AddForce(new Vector2(dirx * speed * Time.deltaTime, diry * speed * Time.deltaTime), ForceMode2D.Impulse);
player.velocity = new Vector2(0, 0);
player.AddForce(new Vector2(-(dirx * playerSpeed * Time.deltaTime), -(diry * playerSpeed * Time.deltaTime)), ForceMode2D.Impulse);
oneTime = true;
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Enemy"))
{
//Destroy(gameObject);
bullet.velocity = new Vector2(0, 0);
gameObject.SetActive(false);
objPooler.poolDictionary["Bullet"].Enqueue(gameObject);
}
}
}
Object Pooler Code -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPooler : MonoBehaviour
{
[System.Serializable]
public class Pool
{
public string tag;
public GameObject prefab;
public int size;
}
public List<Pool> pools;
public Dictionary<string, Queue<GameObject>> poolDictionary;
private Dictionary<string, GameObject> prefabDictionary;
#region Singleton
public static ObjectPooler Instance { get; private set; }
private void Awake()
{
Instance = this;
CreatePoolDict();
}
#endregion
//create pool dictionary
void CreatePoolDict()
{
poolDictionary = new Dictionary<string, Queue<GameObject>>();
prefabDictionary = new Dictionary<string, 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);
prefabDictionary.Add(pool.tag, pool.prefab);
}
}
public GameObject spawnFromPool(string tag, Vector2 position, Quaternion rotation)
{
if (!poolDictionary.ContainsKey(tag))
{
return null;
}
//if one of the pools is empty
if(poolDictionary[tag].Count == 0)
{
AddObjectsToPool(tag, 1);
}
GameObject objectToSpawn = poolDictionary[tag].Dequeue();
objectToSpawn.SetActive(true);
objectToSpawn.transform.position = position;
objectToSpawn.transform.rotation = rotation;
return objectToSpawn;
}
private void AddObjectsToPool(string tag, int num)
{
GameObject prefabToAdd = null;
//get prefab
prefabToAdd = prefabDictionary[tag];
//adding objects to pool
for(int i = 0; i < num; i++)
{
GameObject obj = Instantiate(prefabToAdd);
obj.SetActive(false);
poolDictionary[tag].Enqueue(obj);
}
}
}
Thanks in advance :)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612220307im_/https://answers.unity.com/themes/thub/images/avi.jpg)