Search Unity

Multiple trails following my character

Discussion in 'General Graphics' started by FeastSC2, Aug 22, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I want to spawn mutliple trails, they must all follow my moving character, playing with the particle system and the trail renderer I realized that you can only spawn one trail, or multiple trails but originating from particles.

    Since I want the multiple trails to follow my character and not particles, I can't use trails spawning from particles. What should I do to spawn mutliple trails that follow a moving object?
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    I don't fully understand fully why you can't use particles for this? If you make the particles follow the player (local simulation space, and parented to the character), then the trails will also follow.
     
    theANMATOR2b likes this.
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Yea ok my bad, thanks
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Good luck :)

    You can also hide the particles and show only the trails by setting the Render Mode in the Renderer Module to None.
     
    theANMATOR2b likes this.
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Cool thx!
     
  6. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    One last thing if you don't mind: Can I use a sprite sheet for animations on the trail renderer?
     
  7. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    You can, but the issue is that Particle System's Texture Sheet Animation module doesn't work on particle trails. For that you need a separate script to scroll the trail material.

    Save the following code as "AniamtedTexture.cs" to your project folder and add it to your trail object.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AniamtedTexture : MonoBehaviour {
    6.     [Tooltip("Material to be scrolled. If none is assigned, it will look for trail material of particle system first.")]
    7.     public Material mat;
    8.     [Tooltip("Texture identity of the material to be scrolled. Exact identity can be found in the shader property.")]
    9.     public string texture = "_MainTex";
    10.     [Tooltip("How the UV of the texture is divided. Value smaller than 1 means the UV is repeated which requires 'Wrap Mode' of the texture to be 'repeat'.")]
    11.     public Vector2 tileDivision;
    12.     Vector2 tiling;
    13.     [Tooltip("Scrolling interval in second.")]
    14.     public float scrollTime = 0.1f;
    15.     float t;
    16.  
    17.     void OnEnable() {
    18.         if (mat == null) {
    19.             var psr = GetComponent<ParticleSystemRenderer>();
    20.             mat = psr.trailMaterial;
    21.             if (mat == null) {
    22.                 mat = GetComponent<Renderer>().material;
    23.             }
    24.         }
    25.         reset();
    26.         tiling.x = 1f / tileDivision.x;
    27.         tiling.y = 1f / tileDivision.y;
    28.         mat.SetTextureScale(texture, tiling);
    29.         mat.SetTextureOffset(texture, new Vector2(0, mat.GetTextureOffset(texture).y + tiling.y));
    30.     }
    31.     void Update() {
    32.         t += Time.deltaTime;
    33.         if (t > scrollTime) {
    34.             mat.SetTextureOffset(texture, new Vector2(mat.GetTextureOffset(texture).x + tiling.x, mat.GetTextureOffset(texture).y));
    35.             if (mat.GetTextureOffset(texture).x >= 1) {
    36.                 mat.SetTextureOffset(texture, new Vector2(0, mat.GetTextureOffset(texture).y + tiling.y));
    37.             }
    38.             if (mat.GetTextureOffset(texture).y >= 1) {
    39.                 mat.SetTextureOffset(texture, new Vector2(mat.GetTextureOffset(texture).x, 0));
    40.             }
    41.             t = 0;
    42.         }
    43.     }
    44.     void OnDisable() {
    45.         reset();
    46.     }
    47.     void reset() {
    48.         mat.SetTextureScale(texture, new Vector2(1f,1f));
    49.         mat.SetTextureOffset(texture, new Vector2(0f,0f));
    50.     }
    51. }
     
    Last edited: Aug 23, 2017
    theANMATOR2b and FeastSC2 like this.
  8. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Nice, thanks for the script ifurkend, much appreciated!
    I'll try it right away ;)
     
  9. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
  10. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    I don't usually scroll or flip texture for my trails. If I am making a jet like trail, I will just emit a trail of particles (emission rate over distance) instead of using trails module/renderer.

    The only instance which animated trail is applicable would be making a electric plasma trail to scroll your plasma flipbook texture.
     
    FeastSC2 likes this.
  11. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I'll try it the plasma trail out ifurkend.
    I originally wanted to achieve a tentacle-like trail, where the start of the trail originates from the box (character) and then spreads/fans out more and more towards the end of the trail.

    The problem with trails is that I don't think such behaviours can be achieved. Here's the closest I got: https://drive.google.com/open?id=0B7SRXWCJcuQtQkZpdUlKRk1velU (the noise was a little too high)
    As you can see, in order for the end of the trail to be spread out, the particles need first to be spread out since the beginning of the trail (which I don't want in this case).

    I want that effect to be based on movement, so I thought trail was the way to go but it seems to be a dead end.

    I looked at particles before trying out the trail way, however looking at a few tutorials I saw people don't seem to bother doing effects that would follow erratic movements (because their effects are linear in nature).
    For example: This tutorial
    (watch the first 20s) does a projectile with a core texture, if this texture was to be rotated to follow some entity's movement, it would surely look quite silly because it's a big sprite rotating instead of a trail.

    The core texture: http://i.imgur.com/BloOGsg.png

    I don't know if I'm clear on what I'm trying to achieve, is this something that's generally hard when creating effects or am I missing something?
     
  12. ifurkend

    ifurkend

    Joined:
    Sep 4, 2012
    Posts:
    350
    The texture for composing the particle-trail (without trails module/trail renderer) you need is just a round one. Don't attempt any "long-tail" texture if your projectile is gonna turn.
     
    FeastSC2 likes this.
  13. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    I wouldn't call that a dead end exactly. Although you didn't succeed in the effect you were going for - I can totally see the beginnings of a Villain Carnage type effect here. It's pretty cool imo.
    Carnage from Spiderman - :mad: