Creating a Blast Wave using Line Renderer in Unity

Blast wave is a really cool effect that you can use in your game. Using a line renderer gives a cool horizontal blast effect. In this tutorial, we will see how to create this cool looking effect and the script required to push the objects away when the blast happens.

Here is how the output will look like

Video Tutorial

Creating the Line Renderer Effect

The first step is to create the Line Renderer Effect. For this We will be using some mathematics to find out the points on the circle. Then we will increase the radius of the circle and update the Line Renderer’s position.

To find the positions, we need the angle between each point. You can divide 360 by the number of points to get this.

Unity Black Friday Deals

anglebetweenPoints=360/pointCount;

Next, we need to get the position in the X and Z as our game is in the XZ plane. We can use SIn and Cos of the angles to get the direction and multiply them with the radius to get the position. As the last position should be equal to the first position, we can set it separately.

for(int i=0;i<pointCount-1;i++)
        {
            float angle=anglebetweenPoints*i*Mathf.Deg2Rad;
            positions[i]=new Vector3(Mathf.Sin(angle),0,Mathf.Cos(angle));
        }
 positions[pointCount-1]=positions[0];

We will increment the radius in our update function and set the positions of the Line Renderer.

if(radius<=maxRadius)
        {
            SetPositions();          
            radius+=Time.deltaTime*speed;
            lr.widthMultiplier=(maxRadius-radius)/maxRadius;
        }

Adding Physics Force to move objects

Now that we have the Line Renderer for the visual effect, we need to add forces to objects within the radius to move them.

We will use the Physics overlap sphere function to find the colliders in the specified radius and add force to them. You need to have Rigid body attached to the colliders to apply the force.

Here is how the code looks like

col=Physics.OverlapSphere(transform.position,radius);
        foreach(var i in col)
        {
            i.gameObject.GetComponent<Rigidbody>().AddForce((i.transform.position-transform.position)*forceval,ForceMode.Impulse);
        }

Combining all these codes together and adding the Line renderer to an Empty game object we have our final effect.

Go to the Hierarchy window and create a new Empty game object. Attach the script below to this gameobject and add a Line Renderer. Change the material of the LIne Renderer to the material of your choice and make sure to uncheck “Use World Space”.

Here is the final script

using UnityEngine;

public class BlastScript : MonoBehaviour
{
    LineRenderer lr;
    float radius=0;
    int pointCount=30;
    float anglebetweenPoints=0;
    public float speed=10;
    float maxRadius=30;
    Vector3[] positions=new Vector3[30];
    Collider[] col;
    Rigidbody rb;
    float forceval=1;
    // Start is called before the first frame update
    void Start()
    {
        lr=GetComponent<LineRenderer>();
        lr.enabled=true;
        lr.positionCount=pointCount;
        FindPoints(); 
        
    }
    void FindPoints()
    {
        anglebetweenPoints=360/pointCount;
        for(int i=0;i<pointCount-1;i++)
        {
            float angle=anglebetweenPoints*i*Mathf.Deg2Rad;
            positions[i]=new Vector3(Mathf.Sin(angle),0,Mathf.Cos(angle));
        }
        positions[pointCount-1]=positions[0];

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if(radius<=maxRadius)
        {
            SetPositions();
            ApplyForce();
            radius+=Time.deltaTime*speed;
            lr.widthMultiplier=(maxRadius-radius)/maxRadius;
        }
        else{
            radius=0;
        }
        
    }
    void ApplyForce()
    {
        col=Physics.OverlapSphere(transform.position,radius);
        foreach(var i in col)
        {
            i.gameObject.GetComponent<Rigidbody>().AddForce((i.transform.position-transform.position)*forceval,ForceMode.Impulse);
        }
    }
    void SetPositions()
    {
        for(int i=0;i<pointCount;i++)
        {
            lr.SetPosition(i,positions[i]*radius);
        }
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.