Help creating a animated lightning ray

Hi,

I am new to Unity and Particle system. I am going through the tutorials and I need help creating a lightning effect. I see demos of the effect, but am unable to find a tutorial for the same.

I want to create an effect similar to this.

Can you also tell me which special effects would need scripting to create them(apart from the existing Particle system tools)?
Thanks,
A newbie

Hi Tim,

Thanks for replying. Can u help me locate the Line Renderer class? Still figuring my way around the forum.

Thanks,

My first suggestion is to use the LineRenderer class. Especially if you have a 2D game, you can easily set the correct positions of the line, while animating the mainTextureOffset of your line material to scroll your texture.

You could also use some sort of spritesheet animation to switch between different variations of a lightning to create a better effect.

I personally would only use a Particle for this effect to add some sparks around my LineRenderer.

I hope this helps you on your way.

Greets

Edit

To create a new line renderer, you need to create a new empty GameObject.

Just go to GameObject->Create Empty
Then with the new GameObject selected, click Component->Effects->Line Renderer

Now the line renderer component is added to your gameobject. Assign a material to that component and take a look at the parameters of the LineRenderer. You will see an array exposed Positions. This array is used for the actual positions of your points on your line. Note that it actually doesn’t matter where your “empty gameobject” is placed in the scene, because it’s the Positions-array that defines the positions.

When you want to create a LineRenderer through code, here’s an example:

GameObject newLine = new GameObject();
LineRenderer line = newLine.AddComponent<LineRenderer>();

line.SetPosition(0, linePos1);
line.SetPosition(1, linePos2);
line.SetWidth(_LineWidth, _LineWidth);

If you want to have a line with more than 2 points, you first need to call SetVertexCount to set the correct array size, because the default Component will only have 2 vertices.

This should be enough info for you to get started with LineRenderers.

And if this doesn’t deserve a thumbs up, I don’t know what will :wink:

Cheers

A definite thumbs up from me…Thank You! Will give it a try.

You can simply use lineRenderer or a simple Quad (does not matter) the trick is to open photoshop and create 4-6 frames of lightning, then create the Animation.

However, if you plan to use mainTextureOffset, it will increase your draw calls. And you don’t want that.

I was trying to find a way how to access lineRenderer’s mesh, but I think it will be a private member in lineRenderer class. So my advice is to use Quad… create your own mesh, then animate UV’s of the quad.

Create an empty gameobject, add a line renderer component to it and add the following script to the gameobject.

var targetObject : GameObject;
private var lineRenderer : LineRenderer;

function Start()
{
lineRenderer = GetComponent(LineRenderer);
}

function Update ()
{

if(Input.GetKey(KeyCode.Mouse0))
{

    lineRenderer.enabled = true;

        lineRenderer.SetPosition(0,this.transform.localPosition);

// lineRenderer.SetPosition(1,Vector3(1,1,1));
// lineRenderer.SetPosition(2,Vector3(1,1,35));

        for(var i:int=1;i<4;i++)
         {
            var pos : Vector3 = Vector3.Lerp(this.transform.localPosition,targetObject.transform.localPosition,i/4.0f);

            pos.x += Random.Range(-0.6f,0.6f);
            pos.y += Random.Range(-0.6f,0.6f);

            lineRenderer.SetPosition(i,pos);
        }

        lineRenderer.SetPosition(4,targetObject.transform.localPosition);
}
else
{
    lineRenderer.enabled = false; 
}

}