- Home /
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++;
}
}
Can anyone offer a suggestion on how to get the trail render of the bullet to match the trajectory of the bullet? At present, the TrailRenderer is lagging behind the bullet relative to the direction of travel of the spawn point... this is evidenced both by the trail originating to the left of the weapon (though the bullet is originating at the end of the barrel) when traveling horizontally to the right and the "ricochet" point of the Trail Renderer being different than the impact point of the bullet with the collider surface. I need a visually accurate means of rendering a tracer in the air.
Answer by spinaljack · Jul 22, 2010 at 01:04 AM
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
I changed the logic to preallocate an array of bullets for the weapon and it has "helped" but only marginally.
I note that when I create the instances of the prefab they are all created as independent entities and are not "attached" to the spawn point (i.e. they don't move with the spawn point through space but hang out in space until I set their position and rotation when fired).
I suspect that adds to the lag versus the bullets all moving with the spawn point through space and being set in motion when fired. Is there a way to Instantiate prefabs and have them attached to the spawn point?
Never$$anonymous$$d.... I'm an idiot. ~smile~ I figured it out.
For the most part, it's working now. When I "fire" a bullet, it originates at the spawn point as evidenced by the "flash" in the night vision occurring at the end of the barrel when I turn the "light" on for the round. However, the TrailRenderer component is still starting the trail for the tracer behind the spawn point relative to the direction of motion.
I can't seem to figure out how to get that to visually anchor to the spawn point. The TrailRenderer component is a child of the bullet and appears to lag the bullet the entire flight (i.e. the tracer does not match the impact point).
Answer by spiel2001 · Jul 30, 2010 at 03:52 PM
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.
Answer by e-bonneville · Jul 30, 2010 at 04:49 PM
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.
TrailRenderer will not work as the trail spawns "behind" the projectile relative to the direction of movement which was, more or less, where this thread started.
Changing the projectiles to be preallocated and just setting them in motion when fired fixed the problem with the projectile spawning behind the motion, but the TrailRenderer continues to do that... and badly depending on the rate of motion. This is especially evident when the direction of travel is perpendicular to the direction of fire (such as the door gunner in a Blackhawk)
In addition, if you set a projectile in motion at a speed that's relatively realistic as it relates to firing an automatic weapon, you'll see the TrailRenderer is always lagging the projectile and will intersect the collision surface at a different point than where the projectile itself intersects... which for a "tracer" is a huge issue given that makes the student aim at a point other than where the bullets are actually hitting.
(FWIW: Our company builds simulators for the military: Blackhawk, Abrams, etc... We're currently using Unity to do a proof of concept for a series of platforms)
Oh, I see. Well, I'm sorry I can't be of more help to you.
It's quite alright... I appreciate all input as I am learning as I go with Unity. Thanks for having made the effort :)
Your answer
Follow this Question
Related Questions
Spawning Player After Player Choice. 1 Answer
Disfigured sprite after build. 0 Answers
Missing Variable for transform 1 Answer