I'm having trouble with applying relative-force to a pooled object.
I have an object-pool that has all my bullets ready for re-use. The bullets are disabled while not in use.
when a player shoots his weapon, a bullet is pulled from the objectpool and the following is done on the bullet, in this order:
- SetActive(true)
- transform.position = bulletSpawnPosition
- transform.rotation = rotation of the gun/barrel
- RigidBody.AddRelativeForce(Vector3.forward * speed, ForceMode.Impulse)
This works correct for just one player. When I have another player who also pulls bullets from the objectpool, the bullet seems to keep the rotation of the previous player, so the force is added in the wrong direction.
One thing that I can/could do, is wait for the next frame, between setting the rotation and applying the relative-force, but that feels wrong. Am i missing something? The code below is slightly modified, but narrows down to what's happening.
function Shoot() {
GameObject poolObject = GetObjectFromPool();
poolObject.SetActive(true);
poolObject.transform.position = transform.position;
poolObject .transform.rotation = transform.rotation;
if (poolObject != null)
{
//add force
Rigidbody rBody = poolObject.GetComponent<Rigidbody>();
rBody.AddRelativeForce(Vector3.forward * ProjectileSpeed.Value, ForceMode.Impulse);
}
}