Spawning moving objects at a moving point...

I'm working on a simulator for the Stryker and the Blackhawk using a 50cal on the first vehicle and M60 on the second. I've set up the ammo and have it firing with tracers, glow in night vision, impacts on the ground and all that... everything's working great.

The only problem is that when I set the vehicle in motion, there appears to be a lag. If I spawn the prefab round at the spawn point on the weapon, by the time the object is created and the instance is spawned, the round appears to be coming from a point "behind" the spawn point relative to the direction of travel of the vehicle.

I've been messing with it for a while and for the life of me, I can't see what I'm doing wrong or a way to address the problem. The following code snippet shows the current state of the firing mechanism...

What am I doing wrong? Can anyone help me out here?

(EDIT: I should mention this snippet is located in the Update() method for the player controls manager)

if( firing )
{
    var spawnTransform = spawnPoint.transform;
    var now = Time.realtimeSinceStartup;
    var elapsed = now - lastFire;
    var interval = 1.0 / roundsPerSecond;
    var fireNow = elapsed >= interval;

    if( fireNow )
    {
        var useTracer = (roundsPerTracer > 0 && (roundsFired % roundsPerTracer)) ? false : true;
        var bullet : GameObject;

        if( useTracer ) bullet = spawnPoint.Instantiate( tracerPrefab, spawnTransform.position, spawnTransform.rotation );
        else  bullet = spawnPoint.Instantiate( roundPrefab, spawnTransform.position, spawnTransform.rotation );

        bullet.rigidbody.velocity = spawnPoint.rigidbody.velocity + spawnTransform.TransformDirection( Vector3.up * velocity );

        if( muzzleFlash ) muzzleFlash.Emit();                       
        if( smokeEmitter ) smokeEmitter.Emit();

        if( nightVisionEnabled )
        {
            var light = bullet.GetComponent( Light );

            if( light ) light.enabled = true;
        }

        audioSource.Play();

        lastFire = now;

        roundsFired++;
    }
}

I think it's appearing at the right spot, instantiation lag would halt an update so there wouldn't be frames between where nothing appears. If you want to reduce this lag you can reuse a pool of bullet objects instead of creating and destroying them all the time. You can calculate how many bullet objects you need from the rate of fire of the weapon and the maximum life time of a bullet or the number of bullets in a clip.

Using built in arrays for this is pretty fast but they're fixed length so you'll need to decide beforehand how many you need.

When you fire a bullet just increment the array index to get the next available bullet, move its position to the gun and make it visible or enable the game object. Then after it hits something disable or hide it.

There are also other visual effects you can use to make it appear as though bullets are emerging from a fast moving gun (even if they technically are already):

  • Attach a muzzle flash texture to the gun barrel so it moves with the gun rather than use particle effects.

  • Make the last tracer origin stick on the moving gun (might look a bit odd if you wave the gun about)

Array docs:

http://unity3d.com/support/documentation/ScriptReference/Array.html

For the record, I worked out a better solution. For the benefit of others reading over my shoulder while I learn here, I created a particle emitter and tied it to the projectile. Giving it a force in the positive direction along with a velocity in the same direction and turning off the "Worldspace" option for the emitter created a fairly usable tracer trail.

The down side is that the trail precedes the actual projectile. However, unless you're looking very closely you won't notice it and the render both anchors correctly to the spawn point on the weapon and impacts at the correct place in the 3D space when the collision trigger occurs. It's not perfect, but it is a working solution until I can work out a better one.

The emitter requires a fairly large number of particles to produce a solid trail. Also, the energy level is very small so the particles quickly evaporate along the trail. Using a negative size growth also helped the appearance a good bit. I tried to stretch the particles for the emitter and had no luck with that, so there's more work to be done, but this is a passable start. Nor was I able to get a tracer line that's as long as the real thing. Close enough to pass for training purposes, but clearly not "real"

If anyone has a working example of a realistic tracer emitter and/or script, I'd love to see it though.

Have you tried using the trail renderer? That's exactly what you're looking for. In fact, it was created to simulate trails and tracers so you wouldn't have to.

Edit: I read your comment, and see you tried already. All I can say is, it must be an error on your part, because I've created some passable tracers myself with it.