- Home /
Ghosting effect for sprites
I am making a ghosting effect for when sprites move. It works by having the parent object instantiate a prefab object with the same sprite as the parent object, rendered with some added opacity, and the ghost destroys itself after a bit. The problem is that i want to make this more flexible, as in i can apply this effect to any sprite, currently however, it only works because i have the parent object hardcoded as part of the ghost prefab's code. Is there some way i can instantiate the ghosts as children of whatever instantiated them, and have access to the parent, so that no matter what object i apply this to, it can become its ghost? examples of the code below:
-on the parent object:
void Update()
{
Instantiate(ghostfab)
}
meanwhile, the script inside the ghost prefab which has been instantiated:
public class GhostScript : MonoBehaviour
{
SpriteRenderer sprite;
float timer = 0.12f;
public GameObject WhatIGhost;
// Start is called before the first frame update
void Start()
{
WhatIGhost = GameObject.Find("ball");
//get the renderer of this sprite
sprite = GetComponent<SpriteRenderer>();
//get the object to ghost's sprite
sprite.sprite = WhatIGhost.GetComponent<SpriteRenderer>().sprite;
//set the position to match the object to ghost
transform.position = WhatIGhost.transform.position;
//set the scale to match the object to ghost
transform.localScale = WhatIGhost.transform.localScale;
//set transparency
sprite.color = new Vector4(50, 50, 50, 0.2f);
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if(timer <= 0)
{
Destroy(gameObject);
}
}
}
Answer by sisus_co · Sep 13, 2019 at 07:45 AM
You can instantiate the ghost script as a child by passing in the parent as a second parameter to the Instantiate method.
Also you should not use the Update method here which is called every single frame, but the Start method, so that only one instance of GhostScript is created.
public GhostScript ghostPrefab;
// Start is called before the first frame update
void Start()
{
// Instantiate the ghost prefab as a child of this transform
Instantiate(ghostPrefab, transform);
}
You can then acquire the parent inside the GhostScript using the transform.parent property.
// Start is called before the first frame update
void Start()
{
var WhatIGhost = transform.parent;
...
An alternative way to do this would be to manually call a method that sets up GhostScript, and passing the parent as a parameter, instead of using the Start method.
This way the code would not rely on the parent being assigned before the Start method is called, making it more reliable.
public class Ball : MonoBehaviour
{
public GhostScript ghostPrefab;
void Start()
{
// Instantiate the ghost prefab as a child of this transform
var ghost = Instantiate(ghostPrefab, transform);
ghost.Setup(transform);
}
}
public class GhostScript : MonoBehaviour
{
float timer = 0.12f;
public void Setup(Transform WhatIGhost)
{
//get the renderer of this sprite
var spriteRenderer = GetComponent<SpriteRenderer>();
//get the object to ghost's sprite
spriteRenderer.sprite = WhatIGhost.GetComponent<SpriteRenderer>().sprite;
//set transparency
spriteRenderer.color = new Vector4(50, 50, 50, 0.2f);
}
...
Answer by MosquitoByte · Sep 13, 2019 at 01:01 PM
attempting the first method gives me an error when setting WhatIGhost to transform.parent: "Cannot implicitly convert type 'UnityEngine.Transform' to 'UnityEngine.GameObject'. As for the second, it seems to have mixed both scripts, let me explain to avoid confusion
-Ball with script that instantiates the ghost in update
-Ghostprefab which gets instantiated by the ball, is an empty gameobject with just a spriterenderer, and Ghostscript.cs
Your answer
Follow this Question
Related Questions
How to make a sprite that change its texture when you go behind it? 0 Answers
Camera layering with post processing? 0 Answers
How to create effects that encase objects? 0 Answers
Baked Bloom/Glow (or one layer glow) 1 Answer
Rendering Effects 1 Answer