How to Procedurally Generate A Weapon Trail

preview_player
Показать описание
#unity3d #unityprocedural #unity
In this tutorial video I show you how I went about generating a weapon trail for my Lightsaber procedurally in Unity.

You might be asking why I would create such a thing? Well, I did some research and was unable to find a particle trail effect, or any other trail effects including the line effect which would look like that of a Lightsaber trail.

So, I went ahead and learnt about how meshes are created using vertices and triangles, I did a video on slicing meshes a while back, which used similar background to achieve slicing. By creating a couple of points between frames, I was able to achieve a nice-looking Lightsaber trail effect.

So, after watching this video you should know how to procedurally generate a weapon trail in unity.

___________________________________________________

____________________________________________________
Рекомендации по теме
Комментарии
Автор

How does this not have more views. Other trail tutorials are usually old, or very complicated, but this is too simple. Thank you very much for this.

whiterice
Автор

FOR THOSE WITH PROBLEMS!!!

If your trail is moving twice the speed away from the object its meant to be creating a trail for:
This is because you have the the trail as a child of the target.
The trail mesh points are based on world positions so if it moves all the vertices move with it.
A vertex point of (30, 30, 30) will become (object.position.x+30, object.position.y+30, object.position.z+30)
Thereby creating that effect where it speeds away.
- Your trail mesh should be stationary.

However once youve changed that you might find the trail is dissappearing depending on the angle of your camera.
This is because the mesh pivot is still at the origin while the mesh vertices are gonna be whereever your object is.
The pivot being so far away causes unity to cull it like it was out of view.

A way around this is to use a mesh function RecalculateBounds() on every frame after updating the tris and verts.

Unity then will calculate its culling correctly and keep it visible when its on screen.

These issues and just the amount of data this approach creates and crunches each frame means its probably not the implementation you want in a final game. I'm not experienced enought to really make that call tho.

THEMJMAN
Автор

It’s great to see how easy it is to create a lightsaber once you break down the process!

meeroli
Автор

Thanks for watching guys! If you enjoyed the video please consider leaving a like and subscribing, it really goes a long way!

Tvtig
Автор

This was helpful and served as a good refresher on procedural mesh generation, but I think there’s some room for optimization. Your method creates twelve vertices every frame, with many of those vertices being directly on top of each other. You can achieve the same result by only generating two verts each frame (one each at the current position of the sword’s base and tip), cutting the vertex count down to 1/6 of what it was before. This would still require the same number of tris, but you could also cut those in half by only drawing one side then flipping their normals to always face towards it. Since we’d be reusing vertices between groups of triangles now and wouldn’t want the end of the trail to connect back to the beginning, we should also remove an extra set of triangles. If we’ve set the sword trail to last for 10 frames, these optimizations would decrease the vertex count from 120 to 20 and the triangle count from 40 to 18 while looking identical to the player, making it much more viable for use in a game.

Mitometo
Автор

If you rotate the main object, trail goes away. How is it supposed to lay in player model hand?

ОлегКирьянов-хэ
Автор

You helped make this happen for me. Thanks!

JediMediator
Автор

I'm having a weird issue where the base and tip move incorrectly with the blade's animations. I'm not sure why this is happening

Saber
Автор

The video is great and I could follow along nicely but, after getting everything ready, the trail seems to move twice as far as everything else. I'm not sure what in the code could be causing the issue and I have no idea where to look for it.

cruiser
Автор

nice job and I was wondering if there was a way to convert the generated tails into fbx and store them, using particle systems and mesh to reduce cpu cost😀

xyx
Автор

I did not succeed ((( the trail of the lightsaber was somewhere in the side and I could not fix it :(

TheOGClassic
Автор

Why are the textures of materials so messy when I use a different one? Whenever I try to change the lightsaber trail texture to a thunder, it only transparently glows but the texture isn't there.

piztech
Автор

Bro, do for loops not work for that wall of code?

keyofvoid
Автор

Thanks for the video and source code. I wanted to ask how would i be able to control the lightsaber with input not with the animations ?

ismetcanhasancebi
Автор

i know what is an easy way to do it

trail renderrer component?

kalpolproductions
Автор

Hay, great Video.
i have 1 Question: is this Code possible to work i 2D when Alterd or do i have to aproch it differenlty?

sry for my English i am from Germany🙈
Thanks!

sapphirerose
Автор

Is it not easier to use Unity Trail renderer?

fabianobersovszky
Автор

It is a very original way, and maybe heavy performance due to the number of elements created by code to do so. Congrats anyway

rafarodriguez
Автор

I wouldn't render the vertices in LateUpdate either. I would figure out how fast the animations are or how fast the player character moves and use this script to only fire when the player attacks, make sure it's wrapped in a method that divides the timescale into how fast the animation/player movement is so that even when you're running heavy computations or renders then it won't affect your performance. No one wants glitchy attack animations. Only drawback is it's not highly modular unless you keep track of every one of your animation speeds. Awesome though!

silchasruin