Im trying to make a little effect that leaves behind a trail of “ghosts” of whatever sprite i apply this method to, and so far it works, but only with the object to ghost being hardcoded into the script. the way it works is by having the object use Instantiate() to create an object called ghostfab, a prefab which copies the sprite at a lower opacity and self destruct after a bit using its own script. I need a way to pass in the object that instantiates ghostfab as a parent, or somehow make ghostfab able to find out what instantiated it, so that i can use this on any object i wish, without having to code a different version of this for each object.
Examples below, and thanks in advance.
within the Update function of BallScript:Instantiate(ghostfab);
the ghostscript:
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);
}
}
}
the result: https://i.gyazo.com/d869fc71985a144f58e9b20c39f81811.gif