Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How to make blade special effect in unity

Discussion in 'iOS and tvOS' started by thomas007, Jan 16, 2010.

  1. thomas007

    thomas007

    Joined:
    Jan 16, 2010
    Posts:
    5
    I am wondering is it possible to do this via particle system? I tried to bind particles on the weapon ,when the weapon moves, particles do move with weapon but is it possible to rotate partile image according to the angle of weapon in x,y,z demensions? Like waving the weapon horizontally results in different effect as vertically
     
  2. 1r0nM0nkey

    1r0nM0nkey

    Joined:
    Apr 8, 2009
    Posts:
    131
    You could try the LineRenderer; although I'm not sure if you can turn off the 'billboarding'...

    I have done this before by modifying a particle emitter to stitch particles together, albeit not in Unity. But, depending on the number of quads in your strip, you could even write a small script to do this. You should even be able to trim down and customize SpriteManager to do this for you without too much trouble.

    http://unity3d.com/support/documentation/Components/class-LineRenderer.html
    http://unity3d.com/support/documentation/ScriptReference/LineRenderer.html

    I posted some code here to help get a LineRenderer up and running...
    http://forum.unity3d.com/viewtopic.php?t=40964&sid=3d4f42fd64056eaaf3b94dd2c22e6ad0

    Sprite Manager 1 2:
    http://forum.unity3d.com/viewtopic.php?t=17864
    http://www.anbsoft.com/middleware/sm2/index.htm

    Here is some code that I used for a progress bar... depending on your level of commitment :) it could provide a starting point for you to make your own TrailRenderer for you weapons:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class ProgressBar : LateStarter {
    5.  
    6.     public enum Role {
    7.    
    8.         JSF,
    9.         F18,
    10.         UAV
    11.     }
    12.    
    13.     public Role role;
    14.  
    15.     public float percent;
    16.     public bool  enemy;
    17.    
    18.     float size = 329.0f;
    19.     float percent_last;
    20.  
    21.     MeshFilter   mesh_filter;
    22.     MeshRenderer mesh_renderer;
    23.  
    24.     Vector3[] vertices;
    25.     Vector3[] normals;
    26.     Vector2[] uv;
    27.  
    28.     int[] triangles;
    29.    
    30.     public void reset () {
    31.        
    32.         // HACK: could be cleaner
    33.        
    34.         vertices[0] = Vector3.up * 11.0f;
    35.         vertices[1] = Vector3.up * 11.0f + Vector3.right * 4.0f;
    36.         vertices[2] = Vector3.right * 4.0f;
    37.         vertices[3] = Vector3.zero;
    38.        
    39.         uv[0] = Vector2.up;
    40.         uv[1] = Vector2.up + Vector2.right;
    41.         uv[2] = Vector2.right;
    42.         uv[3] = Vector2.zero;
    43.        
    44.         vertices[1] = Vector3.right * 0.0f * size + Vector3.up * vertices[1].y;
    45.         vertices[2] = Vector3.right * 0.0f * size + Vector3.up * vertices[2].y;
    46.        
    47.         uv[1] = (Vector2.right * (vertices[1].x - vertices[0].x) / 4.0f) + Vector2.up * uv[1].y;
    48.         uv[2] = (Vector2.right * (vertices[2].x - vertices[3].x) / 4.0f) + Vector2.up * uv[2].y;
    49.        
    50.         mesh_filter.mesh.vertices = vertices;
    51.         mesh_filter.mesh.uv = uv;
    52.     }
    53.    
    54.     protected override void initialize () {
    55.        
    56.         base.initialize ();
    57.        
    58.         switch (role) {
    59.            
    60.             case Role.JSF:
    61.             Global.progress_jsf = this;
    62.             break;
    63.            
    64.             case Role.F18:
    65.             Global.progress_f18 = this;
    66.             break;
    67.            
    68.             case Role.UAV:
    69.             Global.progress_uav = this;
    70.             break;
    71.         }
    72.  
    73.         percent = 0.0f;
    74.         percent_last = -1.0f;
    75.        
    76.         vertices  = new Vector3[4];
    77.         normals   = new Vector3[4];
    78.         uv        = new Vector2[4];
    79.         triangles = new int[6];
    80.        
    81.         vertices[0] = Vector3.up * 11.0f;
    82.         vertices[1] = Vector3.up * 11.0f + Vector3.right * 4.0f;
    83.         vertices[2] = Vector3.right * 4.0f;
    84.         vertices[3] = Vector3.zero;
    85.        
    86.         for (int i = 0; i < 4; i++)
    87.             normals[i] = Vector3.forward;  
    88.  
    89.         uv[0] = Vector2.up;
    90.         uv[1] = Vector2.up + Vector2.right;
    91.         uv[2] = Vector2.right;
    92.         uv[3] = Vector2.zero;
    93.                
    94.         triangles[0] = 0;
    95.         triangles[1] = 1;
    96.         triangles[2] = 2;
    97.         triangles[3] = 0;
    98.         triangles[4] = 2;
    99.         triangles[5] = 3;
    100.  
    101.         mesh_filter = gameObject.AddComponent("MeshFilter") as MeshFilter;
    102.         mesh_filter.mesh = new Mesh ();
    103.         mesh_filter.mesh.vertices  = vertices;
    104.         mesh_filter.mesh.normals   = normals;
    105.         mesh_filter.mesh.uv        = uv;
    106.         mesh_filter.mesh.triangles = triangles;
    107.  
    108.         mesh_renderer = gameObject.AddComponent("MeshRenderer") as MeshRenderer;
    109.         mesh_renderer.material = new Material (Shader.Find("line"));
    110.         mesh_renderer.material.mainTexture = Resources.Load("progress_line") as Texture2D;
    111.         mesh_renderer.material.color = new Color (0.0f, 218.0f/255.0f, 235.0f/255.0f, 0.5f);
    112.         mesh_renderer.castShadows = false;
    113.         mesh_renderer.receiveShadows = false;
    114.     }
    115.    
    116.     protected override void Update () {
    117.        
    118.         base.Update ();
    119.        
    120.         if (enemy) {
    121.             mesh_renderer.material.color = Color.red;      
    122.         }
    123.         else {
    124.             if (percent >= 1.0f)
    125.                 mesh_renderer.material.color = Color.green;
    126.             else
    127.                 mesh_renderer.material.color = new Color (0.0f, 218.0f/255.0f, 235.0f/255.0f, 0.5f);
    128.         }
    129.        
    130.         if (percent != percent_last) {
    131.    
    132.             vertices[1] = Vector3.right * percent * size + Vector3.up * vertices[1].y;
    133.             vertices[2] = Vector3.right * percent * size + Vector3.up * vertices[2].y;
    134.            
    135.             uv[1] = (Vector2.right * (vertices[1].x - vertices[0].x) / 4.0f) + Vector2.up * uv[1].y;
    136.             uv[2] = (Vector2.right * (vertices[2].x - vertices[3].x) / 4.0f) + Vector2.up * uv[2].y;
    137.            
    138.             mesh_filter.mesh.vertices = vertices;
    139.             mesh_filter.mesh.uv = uv;
    140.            
    141.             percent_last = percent;
    142.         }
    143.     }
    144. }
    145.  
     
  3. thomas007

    thomas007

    Joined:
    Jan 16, 2010
    Posts:
    5
    thank you so much for the code, I will look into this
     
  4. thomas007

    thomas007

    Joined:
    Jan 16, 2010
    Posts:
    5
    It seems that the trail of weapon in blade of fury can not be achieve of only drawing lines because:
    1. it's should be an image according to the snapshot
    2. this image should be able to rotate to the same angle with weapon
     
  5. Aaron

    Aaron

    Joined:
    Oct 22, 2008
    Posts:
    122
    if your sword or blade is one full mesh just separate the blade part of the mesh from the hilt and reimport into Unity. Then attach a Trail Renderer to the blade portion of the mesh, and tweak until it looks how you want.
     
  6. thomas007

    thomas007

    Joined:
    Jan 16, 2010
    Posts:
    5
    anyone has Trail Renderer ? :(
     
  7. BigRedSwitch

    BigRedSwitch

    Joined:
    Feb 11, 2009
    Posts:
    724
    If I was doing a trail renderer to try to make an effect as seen above, I'd do it in the following way:

    Make a single poly model of a triangle which is the same length as the sword, and about 5 degrees wide. Put the pivot point for this at the "sharp end" of the triangle (the part which is the handle of the sword)

    Create a material which contains an alpha texture which is the colour of the trail. Make the shader "Nature\2 Pass Unlit".

    When the sword swings, instantiate one of the trail pre-fabs with the origin at the sword handle.

    Every frame thereafter, check the angle that the sword has moved, and if it's more than the 5 degrees (width of the trail), spawn a new trail graphic.

    Attach a script to each trail poly which duplicates the material used in the trail and have the update function gradually fade it out. When the alpha is zero, destroy that instance of the trail prefab and it's associated material.

    That should work fine! :)
     
  8. 1r0nM0nkey

    1r0nM0nkey

    Joined:
    Apr 8, 2009
    Posts:
    131
    The above code gives you a starting point for dynamically building and modifying quads; the first steps for a TrailRenderer. All you really need to do is...

    1) create an emitter reference (any xform will do)
    2) build a tri-strip, collapsed onto the reference xform
    3) "pull" quads away from the emitter, each new edge oriented on the ref
    4) once the quad strip is fully extended, reset the verts so that the last quad drops off and becomes the first

    Code (csharp):
    1.  
    2.  
    3.    ***---*---*---*
    4.  + ||| / | / | / | --> trail
    5.    ***---*---*---*
    6.  ^   ^     ^
    7.  |   |     |
    8. ref  |     |        
    9.      |  extended
    10. compressed
    11.  
    12.