AngryBots bullet spawner - Some help to convert to use Rigidbodies?

I’d like to make use of the bullet pool spawning system used in Angry Bots which keeps a pool of bullet objects in the scene for instant use. The system uses translation to move the bullets however which isn’t right for the game I’m working on. So I would like to instead make it use Rigidbodies and move the bullets using AddForce().

I’ve tried firing the bullet from various places, in both the Spawnng script and the Destroying but the results seem to vary. For instance, the bullets either speed up as I’m firing, or the system won’t recycle them correctly.

There are 2 brief scripts which are shown below. One Spawns the bullets and the other fires them (using translation) and them destroys them after a specified time. They have been converted to C# from the original JS in AngryBots with only the relevant sections shown.

Any help would be great - Thanks.

SimpleLaser

using UnityEngine;
using System.Collections;

public class SimpleLaser : MonoBehaviour
{
    public Vector3 Direction = Vector3.zero;

    public float lifeTime = 2f;

    private float spawnTime = 0.0f;

    public bool destroyForMe = false;

    public float ShotForce;

    public void OnEnable()
    {
        Debug.LogWarning("OnEnable"); 

        // The time this object spawned
        spawnTime = Time.time;

        gameObject.rigidbody.velocity = Vector3.zero;
        gameObject.rigidbody.angularVelocity = Vector3.zero;

        // This is the force being added. 
        gameObject.rigidbody.AddForce(Direction * ShotForce); 
    }

    void Update()
    {
        if (Time.time > spawnTime + lifeTime) // ||dist < 0)
        {
            Debug.LogWarning("Spawner.Destroy(gameObject)");

            Spawner.Destroy(gameObject);
        }
    }
}

EnemyBehaviour

    public IEnumerator DoFireAnim()
    {
        // Causes the coroutine to repeat
        while (true)
        {
            Enemy_Anim_obj.animation[roll.name].speed = 2;
            Enemy_Anim_obj.animation.CrossFade(roll.name, 0.2f);

            yield return new WaitForSeconds(RollTime);

            Enemy_Anim_obj.animation.Stop(roll.name);

            Enemy_Anim_obj.animation[open.name].speed = 2;
            Enemy_Anim_obj.animation.CrossFade(open.name, 0.2f);

            if (Enemy_Anim_obj.animation.IsPlaying(open.name))
            {
                // Wait
            }
            else
                Enemy_Anim_objEnemy_Anim_obj.animation.CrossFade(idle.name, 0.2f);

            yield return new WaitForSeconds(0.3f);

            shootingFlag = true;

            Debug.Log("Called Shoot()");

            // Enough time to fire bullets
            yield return new WaitForSeconds(0.6f);

            Enemy_Anim_obj.animation.CrossFade(close.name, 0.2f);
        }
    }

    
    //--------------------------------------------------------
    void FixedUpdate()
    {     
         if (shootingFlag)
         {
             Debug.LogWarning("SHOOTING");


             GameObject newLaserProjectile_L = Spawner.Spawn(LaserProjectile,
                                                         Left_gun.position,
                                                         Left_gun.rotation) as GameObject;

             Vector3 L_GunBulletDir = playerTarget_obj.transform.position - Left_gun.transform.position;

             newLaserProjectile_L.transform.rotation = Quaternion.LookRotation(L_GunBulletDir);

             newLaserProjectile_L.GetComponent<SimpleLaser>().Direction = L_GunBulletDir;
             newLaserProjectile_L.GetComponent<SimpleLaser>().OnEnable();

             shootingFlag = false;
         }         
    }

I haven’t actually looked at angryBots so I’ll just go by what I’m thinking.

If you are going to fire bullets using force instead of controlling it’s position, you’ll need to approach the problem slightly differently.

Instead of fireing the bullet, and then maintaining it’s trajectory/speed within an Update() like the “Fireing and Destroying” scripts seems to do in your example, you probably should create your own script to control the bullet.

Create a script, add it to your bullets. You may want to pass in some variables (direction?). The moment you enable the bullet, it will fire itself straight forward if you don’t change anything.

public void OnEnable()
{
    spawnTime = Time.time;              //time this object spawned
    Vector3 direction = gameObject.rigidbody.forward;    //direction to shoot, modify at will
    float forceToApply = 25;            // higher number = faster travelling bullet.
    gameObject.rigidbody.AddForce(direction * forceToApply ) // This is the force being added.  
} 

public void Update()
{
    // Some code in here to track time and to relocate the bullet to your pool when timeout occurs.
}

Notes:
If you move a rigidbody by relocating it’s position you should also stop it first:

rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;

If you move your bullet too fast, it may travel through any colliders you want it to hit before the game has a chance to trigger the collision.

Let me know if you want more info, I’ll see what I can come up with.

Sources:
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html