Hi all! I'm poking around the "built-in-settings" in Unity - trying to find some sort of "Effect Panel". No luck. I know Unity is not Photoshop but was just hoping for something to mess around with. Lights, effects, shadows, etc... I guess its all programming in the end... I'm new to Unity btw. so I might have missed something. Anyone have tips on how to make an effect that creates a "delayed ghost" of my character, similar to CastleVania. (picture attacked) If its possible to post codes for others to copy, feel free to post them here. Just describe your "effect" and how to implement it!
I wrote this for someone else asking about this awhile back: Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class SpriteAfterImage : MonoBehaviour { [Tooltip("The color each after-image will fade to over its lifetime. Alpha of 0 is recommended")] public Color finalColor = Color.clear; [Tooltip("The amount of time an after-image will take to fade away.")] public float trailLifetime = .25f; [Tooltip("The distance this object must move to spawn one after-image.")] public float distancePerSpawn = .1f; [Tooltip("Optimization - number of after-images to create before the effect starts, to reduce the start-up load.")] public int spawnOnStart = 0; private SpriteRenderer mainSpriteRenderer; // the sprite renderer to trail after private List<SpriteRenderer> readyObjects; // the list of objects ready to be shown private float distanceTraveledSinceLastSpawn; // the distance this object has moved since the last object was shown private Vector3 lastSpawnPosition; // the position the last object was spawned private Color initialColor; private void Awake() { // get the sprite renderer on this object mainSpriteRenderer = GetComponent<SpriteRenderer>(); initialColor = mainSpriteRenderer.color; // initialize the empty list readyObjects = new List<SpriteRenderer>(); // optionally populate list beforehand with objects to use for(int i = 0; i < spawnOnStart; i++) { readyObjects.Add(makeSpriteObject()); } } private void OnEnable() { StartCoroutine(trailCoroutine()); } // function to create a sprite gameobject ready for use private SpriteRenderer makeSpriteObject() { // create a gameobject named "TrailSprite" with a SpriteRenderer component GameObject spriteObject = new GameObject("TrailSprite", typeof(SpriteRenderer)); // parent the object to this object so that it follows it spriteObject.transform.SetParent(transform); // center it on this object spriteObject.transform.localPosition = Vector3.zero; // hide it spriteObject.SetActive(false); return spriteObject.GetComponent<SpriteRenderer>(); } private IEnumerator trailCoroutine() { // keep running while this component is enabled while(enabled) { // get the distance between the current position and the last position // a trail object was spawned distanceTraveledSinceLastSpawn = Vector2.Distance(lastSpawnPosition, transform.position); // if that distance is greater than the specified distance per spawn if(distanceTraveledSinceLastSpawn > distancePerSpawn) { // if there aren't any objects ready to show, spawn a new one if(readyObjects.Count == 0) { // add that object's sprite renderer to the trail list readyObjects.Add(makeSpriteObject()); } // get the next object in the ready list SpriteRenderer nextObject = readyObjects[0]; // set this trailSprite to reflect the current player sprite nextObject.sprite = mainSpriteRenderer.sprite; // this makes it so that the trail will render behind the main sprite nextObject.sortingLayerID = mainSpriteRenderer.sortingLayerID; nextObject.sortingOrder = mainSpriteRenderer.sortingOrder - 1; // set it loose in the world nextObject.transform.SetParent(null, true); // show it nextObject.gameObject.SetActive(true); // start it fading out over time StartCoroutine(fadeOut(nextObject)); // remove it from the list of ready objects readyObjects.Remove(nextObject); // save this position as the last spawned position lastSpawnPosition = transform.position; // reset the distance traveled distanceTraveledSinceLastSpawn = 0; } // wait until next frame to continue the loop yield return null; } // reduce number of sprites back to original pool size foreach(SpriteRenderer sprite in this.readyObjects) { if(this.readyObjects.Count > spawnOnStart) { Destroy(sprite.gameObject); } else { resetObject(sprite); } } } private IEnumerator fadeOut(SpriteRenderer sprite) { float timeElapsed = 0; // while the elapsed time is less than the specified trailLifetime while(timeElapsed < trailLifetime) { // get a number between 0 and 1 that represents how much time has passed // 0 = no time has passed, 1 = trailLifetime seconds has passed float progress = Mathf.Clamp01(timeElapsed / trailLifetime); // linearly interpolates between the initial color and the final color // based on the value of progress (0 to 1) sprite.color = Color.Lerp(initialColor, finalColor, progress); // track the time passed timeElapsed += Time.deltaTime; // wait until next frame to continue the loop yield return null; } // reset the object so that it can be reused resetObject(sprite); } // resets the object so that it is ready to use again private void resetObject(SpriteRenderer sprite) { // hide the sprite sprite.gameObject.SetActive(false); // reset the tint to default sprite.color = initialColor; // parent it to this object sprite.transform.SetParent(transform); // center it on this object sprite.transform.localPosition = Vector3.zero; // add it to the ready list readyObjects.Add(sprite); } } Put this on anything with a sprite renderer, and it configure it in the inspector. It can be enabled/disabled to control when the effect is active. Right now the initial color matches the target spriterenderer's tint, but that could be changed to a configurable color. I commented the crap out of it, so if you want to learn, read through the code and see if you can understand what's going on. I'd be happy to answer any questions you have about it.
Wow! It actually worked! Exactly what I was looking for. The SpriteAfterEffect is big and moving in opposite direction... I will look into your code when I have time and see if I can crack it. Thank you soo much though!
No problem! Glad it works for you. You'll need to copy the local scaling from your character to the sprites as they're shown. I would do this in the "trailCoroutine", right next to where the sprite is set. "mainSpriteRenderer" is the reference to your character's SpriteRenderer, so adding "nextObject.transform.localScale = mainSpriteRenderer.transform.localScale;" would do it i think.
thanks! I copied "nextObject.transform.localScale = mainSpriteRenderer.transform.localScale;" into the script, inderneath nextObject.sortingLayerID = mainSpriteRenderer.sortingLayerID; nextObject.sortingOrder = mainSpriteRenderer.sortingOrder - 1; and now the "SpriteAfterEffect" is tiny now... Not sure how/where to write the "local scaling"-sentence in "trailCorotine" tough...
Well, depending on how your character is getting it's size, (through several different parent object scales, etc) you may need to change how you get the scale. I have a few ideas for how to fix that. First lets try this: Leave that line where it is, and in the function "makeSpriteObject", change the "SetParent(transform)" to "SetParent(mainSpriteRenderer.transform.parent)". That way the after-images have the same parent as the character, and by copying the character's local scale, the after-images should be the same size.
W Seem to work, kinda... Its looking better but still has those small animations as you can see. And It only works ONCE, meaning, first time I jump it leaves a nice effect in proper size, combined with the smaller ones. But second time I jump it only leave the smaller effects, (those small dots) Im very noob at programing still, I should actually learn myself... But Im currently working on dialog boxes so... Unless you have time to spare, ignoe me and I will try me best to solve it myself! But thanks anyway!
I'm happy to try and help you fix this. Basically we just need the copies to have the same visible scale as your sprite when they are spawned. Try this code where I've added a line to copy the world-scale of the sprite: Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class SpriteAfterImage : MonoBehaviour { [Tooltip("The color each after-image will fade to over its lifetime. Alpha of 0 is recommended")] public Color finalColor = Color.clear; [Tooltip("The amount of time an after-image will take to fade away.")] public float trailLifetime = .25f; [Tooltip("The distance this object must move to spawn one after-image.")] public float distancePerSpawn = .1f; [Tooltip("Optimization - number of after-images to create before the effect starts, to reduce the start-up load.")] public int spawnOnStart = 0; private SpriteRenderer mainSpriteRenderer; // the sprite renderer to trail after private List<SpriteRenderer> readyObjects; // the list of objects ready to be shown private float distanceTraveledSinceLastSpawn; // the distance this object has moved since the last object was shown private Vector3 lastSpawnPosition; // the position the last object was spawned private Color initialColor; private void Awake() { // get the sprite renderer on this object mainSpriteRenderer = GetComponent<SpriteRenderer>(); initialColor = mainSpriteRenderer.color; // initialize the empty list readyObjects = new List<SpriteRenderer>(); // optionally populate list beforehand with objects to use for(int i = 0; i < spawnOnStart; i++) { readyObjects.Add(makeSpriteObject()); } } private void OnEnable() { StartCoroutine(trailCoroutine()); } // function to create a sprite gameobject ready for use private SpriteRenderer makeSpriteObject() { // create a gameobject named "TrailSprite" with a SpriteRenderer component GameObject spriteObject = new GameObject("TrailSprite", typeof(SpriteRenderer)); // parent the object to this object so that it follows it spriteObject.transform.SetParent(transform); // center it on this object spriteObject.transform.localPosition = Vector3.zero; // hide it spriteObject.SetActive(false); return spriteObject.GetComponent<SpriteRenderer>(); } private IEnumerator trailCoroutine() { // keep running while this component is enabled while(enabled) { // get the distance between the current position and the last position // a trail object was spawned distanceTraveledSinceLastSpawn = Vector2.Distance(lastSpawnPosition, transform.position); // if that distance is greater than the specified distance per spawn if(distanceTraveledSinceLastSpawn > distancePerSpawn) { // if there aren't any objects ready to show, spawn a new one if(readyObjects.Count == 0) { // add that object's sprite renderer to the trail list readyObjects.Add(makeSpriteObject()); } // get the next object in the ready list SpriteRenderer nextObject = readyObjects[0]; // set this trailSprite to reflect the current player sprite nextObject.sprite = mainSpriteRenderer.sprite; // this makes it so that the trail will render behind the main sprite nextObject.sortingLayerID = mainSpriteRenderer.sortingLayerID; nextObject.sortingOrder = mainSpriteRenderer.sortingOrder - 1; // set it loose in the world nextObject.transform.SetParent(null, true); // match the copy's scale to the sprite's world-space scale nextObject.transform.localScale = mainSpriteRenderer.transform.lossyScale; // show it nextObject.gameObject.SetActive(true); // start it fading out over time StartCoroutine(fadeOut(nextObject)); // remove it from the list of ready objects readyObjects.Remove(nextObject); // save this position as the last spawned position lastSpawnPosition = transform.position; // reset the distance traveled distanceTraveledSinceLastSpawn = 0; } // wait until next frame to continue the loop yield return null; } // reduce number of sprites back to original pool size foreach(SpriteRenderer sprite in this.readyObjects) { if(this.readyObjects.Count > spawnOnStart) { Destroy(sprite.gameObject); } else { resetObject(sprite); } } } private IEnumerator fadeOut(SpriteRenderer sprite) { float timeElapsed = 0; // while the elapsed time is less than the specified trailLifetime while(timeElapsed < trailLifetime) { // get a number between 0 and 1 that represents how much time has passed // 0 = no time has passed, 1 = trailLifetime seconds has passed float progress = Mathf.Clamp01(timeElapsed / trailLifetime); // linearly interpolates between the initial color and the final color // based on the value of progress (0 to 1) sprite.color = Color.Lerp(initialColor, finalColor, progress); // track the time passed timeElapsed += Time.deltaTime; // wait until next frame to continue the loop yield return null; } // reset the object so that it can be reused resetObject(sprite); } // resets the object so that it is ready to use again private void resetObject(SpriteRenderer sprite) { // hide the sprite sprite.gameObject.SetActive(false); // reset the tint to default sprite.color = initialColor; // parent it to this object sprite.transform.SetParent(transform); // center it on this object sprite.transform.localPosition = Vector3.zero; // add it to the ready list readyObjects.Add(sprite); } }
yes! It worked! Nailed it Thank you soo much! Now I will just decide collor of effect and distance etc