using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.VFX; [ExecuteAlways] public class SwarmAnimator : MonoBehaviour { [SerializeField] private VisualEffect vfx; private Vector3 currentPosition; private Vector3 previousPosition; [SerializeField] private Rigidbody rb; [SerializeField] private string parameterName = "DeltaVector"; [SerializeField] private Vector3 avoidancePosDefault; [SerializeField] private float explodeDuration = 1.0f; private float dur = 0f; private bool isExploding = false; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { previousPosition = currentPosition; currentPosition = transform.position; Vector3 velocity = (currentPosition - previousPosition) ; vfx.SetVector3("DeltaVector", velocity); if (Input.GetKeyDown(KeyCode.Space)) { Explode(); } if (isExploding) { if(dur> explodeDuration) { StopExplode(); } dur += Time.deltaTime; } } void Explode() { vfx.SetVector3("Avoid", transform.position); this.isExploding = true; dur = 0; //Wait a sec then stop. } void StopExplode() { vfx.SetVector3("Avoid", avoidancePosDefault); this.isExploding = false; vfx.Stop(); } private void FixedUpdate() { } }