Skip to content

Instantly share code, notes, and snippets.

@SamyBencherif
Created December 9, 2024 14:50
Show Gist options
  • Save SamyBencherif/89045e035d4f094d71d56ca9ff166c0c to your computer and use it in GitHub Desktop.
Save SamyBencherif/89045e035d4f094d71d56ca9ff166c0c to your computer and use it in GitHub Desktop.
Plays effects made using Visual Effect Graph in Unity backwards
using UnityEngine;
using UnityEngine.VFX;
public class VFXReverse : MonoBehaviour
{
public float startTime = 5;
float time;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
//GetComponent<VisualEffect>().Stop();
GetComponent<VisualEffect>().resetSeedOnPlay = false;
time = startTime;
}
// Update is called once per frame
void Update()
{
const int iterations = 100;
if (time > 0)
{
GetComponent<VisualEffect>().Reinit();
for (int i = 0; i<iterations; i++)
GetComponent<VisualEffect>().Simulate(time/iterations);
time -= Time.deltaTime;
}
else if (time > -2)
{
GetComponent<VisualEffect>().Reinit();
time -= Time.deltaTime;
}
else
{
time = startTime;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment