I don't think anyone has asked this question but how exactly would a laser work and if there is a way how would you design a sprite or texture for it?

Off the top of my head, render a raycast in screenspace additively with wanted hue saturation and value..?

I did some tests about that. Technically it's a set of 2 rectangles, the border and the center with each their own color (usually the center is white), and the whole thing is blurred. This works as well in 2D as in 3D (somehow).

(For 2D lasers)

The texture can be a cross-section of the laser beam, and you just scale it in one direction so it's as long as you need it to be. Then, if you don't want it to have a sharp cutoff, you place end textures at either end. So, basically a 9-slice texture.

http://i.imgur.com/oo6RBSk.gif

If you set the sprite offset so its origin is at one end, you can simply scale it. If it's origin is in the center, just calculate the center point and set its position every frame that it's active.

For the mechanics: Do a ray cast from your gun muzzle to some maximum range point. Get the distance between the muzzle and the hit point (or the max range point if there is no hit), and scale the texture based on that distance. You'll get a hit object from the raycast, so you can do damage to that. If you want it to pass through things and hit multiple objects, I've used an Area2D that's also scaled to length, and apply damage to anything in the area.

3 years later

@Gokudomatic said: I did some tests about that. Technically it's a set of 2 rectangles, the border and the center with each their own color (usually the center is white), and the whole thing is blurred. This works as well in 2D as in 3D (somehow).

I'm new on shaders, does anyone know some resources, or advices for the blur looking ?

To add some context, my game draws 2D lines dynamically, but they have a boring looking, so I want to try a neon effect.

@NeoD You can simply create an image that has a "glow" layer added to it, and use it in a Sprite or NinePatchRect node. This layer can be baked into the sprite or made separate (if you'd like to control its color/intensity independently from the main beam).

For curved lines, the Line2D node can also be used.

3 years later