- Home /
Any idea on this flash/charge effect
Hello there,
I'm trying to make a similar effect like in the visual below. There are 2-3 flash characters appearing and fading out while flash is running. Green arrows on the image is showing what I mean. Is it possible to do similar kind of effect on characters without consuming too much CPU ?

Helps are greatly appreciated
Thanks
You can just make another object (or as many as you want) follow your player with a small delay in the animation.
Or have objects sampling your player's animation and position and fading out. (use a pool of objects in this case)
Any other idea than spawning other objects? Like any options with shaders?
Answer by robertbu · Sep 16, 2014 at 01:28 AM
Here is some code that uses quads in a mesh rather than individual game objects to create the duplicates. I found the setup fussy and required a trail renderer as well to make things look right. It uses two AnimationCurves...one to define how the duplicates space out, and one to define the transparency. Note the code requires a shader that supports vertex colors. Rather than give a detailed explanation of the setup, you will find a Unity Package here:
www.laughingloops.com/Flash.unitypackage
 using UnityEngine;
 using System.Collections;
 
 public class QuadFollow : MonoBehaviour {
 
     public Material material;
     public int quadCount = 3;
     public AnimationCurve acTransparency;
     public AnimationCurve acSpacing;
     public float spacingFactor = 0.5f;
     public float speed = 12.0f;
 
     private Mesh mesh;
     private float[] lerpingFactor;
     private Vector3[] offsets;
     private Vector3 prevPos;
 
     private Vector3[] vertices;
 
     private Vector3 v0 = new Vector3(-0.5f, 0.5f, 0);
     private Vector3 v1 = new Vector3(-0.5f,-0.5f, 0);
     private Vector3 v2 = new Vector3( 0.5f,-0.5f, 0);
     private Vector3 v3 = new Vector3( 0.5f, 0.5f, 0);
 
     void Start () {
         lerpingFactor = new float[quadCount];
         offsets = new Vector3[quadCount];
 
         for (int i = 0; i < quadCount; i++) {
             lerpingFactor[i] = acSpacing.Evaluate ((float)i / (quadCount - 1.0f)) * speed;
         }
 
         MeshFilter mf = GetComponent<MeshFilter>();
         if (mf == null) {
             mf = gameObject.AddComponent<MeshFilter>();
         }
 
         MeshRenderer mr = gameObject.AddComponent<MeshRenderer>();
         mr.material = material;
 
         mesh = new Mesh();
         mesh.MarkDynamic();
         mf.mesh = mesh;
 
         vertices = new Vector3[quadCount * 4];
         int[] triangles = new int[quadCount * 2 * 3];
         Color[] colors = new Color[quadCount * 4];
         Vector2[] uvs = new Vector2[quadCount * 4];
 
 
 
         for (int j = 0; j < quadCount; j++) {
             vertices[j * 4    ] = v0;
             vertices[j * 4 + 1] = v1;
             vertices[j * 4 + 2] = v2;
             vertices[j * 4 + 3] = v3;
         }
 
         for (int j = 0; j < quadCount; j++) {
             
             triangles[j * 6 + 0] = j * 4 + 0;    //     0_ 3        0 ___ 3
             triangles[j * 6 + 1] = j * 4 + 3;    //   | /         |    /|
             triangles[j * 6 + 2] = j * 4 + 1;    //  1|/            1|/__|2
             
             triangles[j * 6 + 3] = j * 4 + 3;    //       3
             triangles[j * 6 + 4] = j * 4 + 2;    //    /|
             triangles[j * 6 + 5] = j * 4 + 1;    //  1/_|2
         }
 
         for (int j = 0; j < vertices.Length / 4; j++) {
             uvs[j * 4 + 0] = new Vector2(0,1);
             uvs[j * 4 + 1] = new Vector2(0,0);
             uvs[j * 4 + 2] = new Vector2(1,0);
             uvs[j * 4 + 3] = new Vector2(1,1);
         }
 
         Color c = Color.white;
         for (int i = 0; i < quadCount; i++) {
             c.a = acTransparency.Evaluate ((float)i/(quadCount - 1.0f));
             colors[i * 4    ] = c;
             colors[i * 4 + 1] = c;
             colors[i * 4 + 2] = c;
             colors[i * 4 + 3] = c;
         }
 
         mesh.vertices = vertices;
         Debug.Log (vertices.Length);
         mesh.triangles = triangles;
         mesh.colors = colors;
         mesh.uv = uvs;
         mesh.RecalculateNormals();
 
         prevPos = transform.position;
     }
     
     void Update () {
         Vector3 delta = prevPos - transform.position;
         prevPos = transform.position;
         for (int i = 0; i < quadCount; i++) {
             offsets[i] += delta;
             offsets[i] = Vector3.MoveTowards (offsets[i], Vector3.zero, Time.deltaTime * lerpingFactor[i]);
             vertices[i * 4    ] = offsets[i] + v0;
             vertices[i * 4 + 1] = offsets[i] + v1;
             vertices[i * 4 + 2] = offsets[i] + v2;
             vertices[i * 4 + 3] = offsets[i] + v3;
         }
         mesh.vertices = vertices;
     }
 }
If your original object is 2D, it would be possible to fold the display of the original plus the the display of the trail into this code, so the result would be one mesh for everything (original object, duplicates and trail), which would draw in a single draw call.
How can we horizontally reverse the character? Like when sprite is reversed, followers also reverse
Answer by Cherno · Sep 16, 2014 at 01:44 AM
There are shaders like the Silhouette Outline one on the Unity Wiki that show a copy of the mesh, maybe you can hack one of these so it shows two or three copies of the mesh offset to the back of the object.
Your answer
 
 
             Follow this Question
Related Questions
VHS style tearing effect 0 Answers
Muzzle Flash 1 Answer
How to trace the movement of player animation? 0 Answers
How can I create glowing trail effect? 0 Answers
Trail renderer problem 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                