Do Rigidbodies cache their next position?

I’ve created a pool of projectiles controlled by physics that I keep track of in a static Dictionary<GameObject, bool> stored in the class for the objects that fire the projectiles so that when something needs one of these objects, it can check the pool to see if any of the projectiles have been recaptured and respawn them at their own position. But any time I have more than two firing objects, if I try to reuse a projectile, it will move to the firing location and then between LateUpdate and the next FixedUpdate, it will move back to its original location.

Relevant pool-usage code below. Please let me know if you think you need more information.

public class Shooter : GridItem, CanBeHitByLaser
{
    private static System.Collections.Generic.Dictionary<GameObject, bool> liveProjectiles = new System.Collections.Generic.Dictionary<GameObject, bool> ();

    public GameObject projectilePrefab;

    public void OnLaserHit (LaserHitParams laserHitParams)
    {
        foreach (GameObject liveProjectile in liveProjectiles.Keys)
        {
            if (!liveProjectiles[liveProjectile])
            {
                //There is a projectile available in the pool.
                FireProjectile (liveProjectile, laserHitParams);
                return;
            }
        }
        //No live projectiles available, must create one.
        GameObject instantiatedProjectile = (GameObject)GameObject.Instantiate (projectilePrefab, transform.position, Quaternion.identity);
        liveProjectiles.Add (instantiatedProjectile, true);
        FireProjectile (instantiatedProjectile, laserHitParams);
    }

    private void FireProjectile (GameObject projectileToFire, LaserHitParams laserHitParams)
    {
        projectileToFire.SetActiveRecursively (true);
        projectileToFire.rigidbody.WakeUp ();
        projectileToFire.rigidbody.velocity = Vector3.zero;
        liveProjectiles[projectileToFire] = true;
        projectileToFire.transform.position = transform.position;
        projectileToFire.SendMessage ("OnLaserHit", laserHitParams, SendMessageOptions.DontRequireReceiver);
    }

    private void CaptureProjectile (GameObject projectile)
    {
        if (liveProjectiles.ContainsKey (projectile))
        {
            projectile.rigidbody.velocity = Vector3.zero;
            projectile.transform.position = Vector3.zero;
            projectile.rigidbody.position = Vector3.zero;
            projectile.SetActiveRecursively (false);
            liveProjectiles[projectile] = false;
        }
        else
        {
            GameObject.Destroy (projectile);
        }
    }
}

Video of the effect I’m seeing can be found here. If anyone’s seen something like this or has an explanation of what I can do to properly reset a rigidbody when deactivating its GameObject, please let me know.

Sure its not some code on the projectile thats causing this?

It’s possible. The “OnLaserHit” of the projectile starts a coroutine that adds force, but that ought to be stopped when the projectile’s disabled. Here’s the entirety of the projectile code:

public class Projectile : MonoBehaviour, CanBeHitByLaser
{
    public int forceUpdates = 10;

    public void OnLaserHit (LaserHitParams laserHit)
    {
        StartCoroutine (AddLaserForce (laserHit));
    }

    public void OnDisable ()
    {
        StopAllCoroutines ();
    }

    private System.Collections.IEnumerator AddLaserForce (LaserHitParams laserHit)
    {
        int elapsedUpdates = 0;
        while (elapsedUpdates < forceUpdates)
        {
            rigidbody.AddForceAtPosition (laserHit.laser.direction * laserHit.chargeMultiplier, laserHit.hitPoint);
            elapsedUpdates++;
            yield return new WaitForEndOfFrame ();
        }
    }
}

As I mentioned before, if I put in debugs in this class showing the position of the projectile in Update, FixedUpdate, and LateUpdate, the projectile appears to move to the desired firing location for one frame, and then between LateUpdate and the next Update, it pops back to the origin. If anything is wonky, I can’t see it, but I appreciate the extra eyes on it.

Adding force you should really do WaitForFixedUpdate, as that is the physics rate.

If it’s doing it between LateUpdate and Update it does sound like the Coroutine could be the culprit, as WaitForEndOfFrame will be processed between those points. If it was an error in physics it could remain in place for multiple frames, as the Physics update at a different rate from the Render Updates (normally slower).

Try FixedUpdate first, and then double check that it is correctly ending all coroutines without adding in another frame (as the next frame would process upon re-enabling the Component, possibly resulting in the effect you are seeing).

I’ve actually gone back and forth between doing it with WaitForFixedUpdate() and WaitForEndOfFrame(). Either way, I see the same result. I also altered the FireProjectile method to be a coroutine that positions the projectile, waits for FixedUpdate and then reactivates the projectile and sends the OnLaserHit message, like so…

private System.Collections.IEnumerator FireProjectile (GameObject projectileToFire, LaserHitParams laserHitParams)
{
    projectileToFire.rigidbody.WakeUp ();
    projectileToFire.rigidbody.velocity = Vector3.zero;
    projectileToFire.rigidbody.MovePosition (transform.position);
    liveProjectiles[projectileToFire] = true;
    yield return new WaitForFixedUpdate ();
    projectileToFire.SetActiveRecursively (true);
    projectileToFire.SendMessage ("OnLaserHit", laserHitParams, SendMessageOptions.DontRequireReceiver);
}

Even doing that, if I add a debug in each update of AddLaserForce, the first iteration through the while loop says the position is the world origin, then the next update is at the firing location, and then the update after that is slightly offset from the origin.