Bullet Trails

Hey All,

Normally I wouldn’t come to you guys with such an aesthetic question… but I’m really overstressed and stumped at the moment. Anyway, what I’m trying to do is make glowing bullet trails, ideally the one’s I’d like to model after are the Combine bullet trails in Half Life 2. My problem is, I have no idea where to start. Should the trail be part of the bullet Mesh? Do I use a particle system? Something altogether different? Normally I’d take the time to figure it out on my own… but I’m graduating soon and I want to get as much done on this game as possible, so any help would be greatly appreciated. Thanks in advance guys.

Have you tried the Trail Renderer?

http://unity3d.com/Documentation/Components/class-TrailRenderer.html

-Jon

Whoa… I had no idea that even existed… well, I thoroughly feel like an idiot now. Thanks a lot though Jon, I appreciate it.

Don’t… Unity is huge and it’s easy to miss something. It doesn’t help that OTEE adds so much each release, either. Glad I could help! :slight_smile:

Cheers,
-Jon

The TrailRenderer connects the positions of an object over several frames. This means it’s good for missiles other stuff that flies. However, for machine-gun like bullets, you really want them faster than that.

IIRC, the combine bullets don’t really move between frames - they are more like flickering randomly along the general line of fire. In order to do that, you should probably use the LineRenderer rather than the TrailRenderer.

The linerenderer needs a small script telling it where to draw.

Wow, thanks. Uh… out of morbid curiosity, what would that script look like in general? (I’m more an artist than a coder, but I’m trying to learn)

You could also use a particle effect with Stretched Particles and Velocity Scale, (in Particle Renderer)

The effect is code-free but you wouldn’t have the same control as Nicholas’ suggestion. (Say, as you fire the effect may be partially visible on the gun barrel)

-Jon

Well, I’ll probably screw around with both, since you don’t actually see the gun barrel’s here (you’re in a cockpit and the cannons are below you). Thank you both for your help.

If you really want an effect like the carbine bullets in halo you should use the line renderer. You can get point one from the gun barrel, and point two from a Raycast going forward from the gun barrel.

var linePrefab : GameObject;
var hitPrefab : GameObject;

var range = 0.00;
var damage = 0.00;

private var hit : RaycastHit;

function Shoot ()
{
	newLineObject = Instantiate(linePrefab);
	newLine = newLineObject.GetComponent(LineRenderer);

	if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit, range))
	{
		newLine.SetPosition(0, transform.position);
		newLine.SetPosition(1, hit.point);

		if(hitPrefab) Instantiate(hitPrefab, hit.point, Quaternion.LookRotation(hit.normal);

		hit.collider.gameObject.SendMessage("Damage", damage, SendMessageOptions.DontRequireReceiver);
	}
	else
	{
		newLine.SetPosition(0, transform.position);
		newLine.SetPosition(1, transform.TransformPoint(Vector3.forward * range));
	}
}

Just make a prefab of a line renderer, make sure it is set to world space (moving it around doesn’t move the line) and it has two positions (this is the default) and put that prefab in linePrefab. range should probably be a pretty big number as this is a laser and hitPrefab and damage are optional. For damage you need a damage reciever script with this in it:

var health = 100.00;
var explosionPrefab : GameObject;

function Damage (damage : float)
{
    health -= damage;
}

function Update ()
{
    if(health < 0)
    {
        Instantiate(explosionPrefab, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}

Call Shoot() from Update() depending on some type of input. If you are lost, consult the docs.

nice yog. that looks pretty clean.

I got something to the effect working now, thanks guys.

Sorry to resurrect an old thread but this is exactly what I need. Trouble is this code give me an error. The line throwing up the error is:

 if(hitPrefab) Instantiate(hitPrefab, hit.point, Quaternion.LookRotation(hit.normal);

If I comment it out the script works fine (except obviously it doesn’t instantiate the hitPrefab any more).

Anyone see why I’m getting this error? I’ve got prefabs for the hitPrefab and linePrefab assigned.

It looks like you’re missing a closing “)”.

 if(hitPrefab) 
Instantiate(hitPrefab, hit.point, Quaternion.LookRotation(hit.normal);

should be:

 if(hitPrefab) 
Instantiate( hitPrefab, hit.point, Quaternion.LookRotation(hit.normal) );
1 Like