I want to create a trailing, ghosting like effect of a sprite that's moving fast. Something very similar to this image of Sonic (apologies of bad quality, it's the only example I could find of the effect I'm looking to achieve)
However, I don't want to do this at the sprite sheet level, to avoid having to essentially double (or possibly quadruple) the amount of sprites in my atlas. It's also very labor intensive.
So is there any other way to achieve this effect? Possibly by some shader voodoo magic? I am using Unity and 2D Toolkit, if that helps.
Answer
While the particle system solution provided by LVBen does work, it's not the best suited solution when using 2D Toolkit for your sprites. The primary reason being is that it's impossible to sync up the ghost trail material in the particle system to the current sprite animation of the main prefab.
Here's the 2D Toolkit friendly solution I ended up using.
For the prefab in which you want the ghost trail to come from, attach an empty game object to it to act as the root. Under this root, attach any number of tk2dSprite or tk2dSpriteAnimator (depending if you want animated sprites or not) game objects (I added 4) and adjust their color alpha values as appropriate to achieve the ghosting/fading away effect.
In the top parent Update
// AmountToMove is a Vector3 of the amount we will translate this gameobject.
float y = (int)AmountToMove.y == 0 ? 0 : -AmountToMove.y;
float distanceFactor = 0.05f;
for (int i = 0; i < GhostingRoot.childCount; ++i) {
// Based on the player's current speed and movement along the x and y axes,
// position the ghost sprites to trail behind.
Vector3 ghostSpriteLocalPos = Vector3.Lerp(
GhostingRoot.GetChild(i).localPosition,
new Vector3((-CurrentSpeed * distanceFactor * i),
(y * distanceFactor * i), 0),
10f * Time.deltaTime);
// GhostingRoot is the root gameobject that's parent to the ghost sprites.
GhostingRoot.GetChild(i).localPosition = ghostSpriteLocalPos;
// Sync the animations.
// _ghostSprites is a List of the tk2dSpriteAnimator ghost sprites.
_ghostSprites[i].Play(SpriteAnimator.CurrentClip.name);
_ghostSprites[i].Sprite.FlipX = Sprite.FlipX;
}
This solution will create the trailing ghosting effect while syncing the animations of the ghost sprites with the main sprite.
No comments:
Post a Comment