using UnityEngine; using UnityEngine.VFX; using RR.Game2D.Behaviour; using RR.Game2D.Core.Managers; namespace RR.Game2D.FX { [RequireComponent(typeof(VisualEffect))] public class PlayerRunVFX : MonoBehaviour { [SerializeField] private VisualEffect runVFX; [SerializeField] private Vector2 offset = Vector2.zero; [SerializeField] private float requiredPlayerRunTime; private GameObject _player; private Rigidbody2D _playerRB; private PlayerController _playerController; private float _startRunTime = 0; private Vector3 particlesVelocityDirection; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { _player = GameManager.Instance.PlayerObj; _playerRB = _player.GetComponent(); _playerController = _player.GetComponent(); if (_player == null) _player = GameObject.FindGameObjectWithTag("Player"); particlesVelocityDirection = runVFX.GetVector3("ParticlesVelocityDirection"); runVFX.Stop(); // runVFX.enabled = false; } // Update is called once per frame void Update() { if (_playerController.IsGrounded) transform.position = _player.transform.position + new Vector3(offset.x * Mathf.Sign(_playerRB.linearVelocityX), offset.y, 0f); else transform.position = new Vector3(_player.transform.position.x, transform.position.y, transform.position.z); if (!runVFX.enabled) runVFX.enabled = true; if (Mathf.Abs(_playerRB.linearVelocityX) >= 0.01f && _playerController.IsGrounded) { if (_startRunTime == 0) _startRunTime = Time.time; if (Time.time - _startRunTime < requiredPlayerRunTime) return; runVFX.Play(); runVFX.SetVector3("ParticlesVelocityDirection", new Vector3(particlesVelocityDirection.x * Mathf.Sign(_playerRB.linearVelocityX), particlesVelocityDirection.y, particlesVelocityDirection.z)); return; } if (_playerRB.linearVelocityY >= 0.25f * _playerController.jumpVelocity) { runVFX.SetVector3("ParticlesVelocityDirection", new Vector3(particlesVelocityDirection.x * Mathf.Sign(_playerRB.linearVelocityX), _playerRB.linearVelocityY, particlesVelocityDirection.z)); runVFX.Play(); if (_playerRB.linearVelocityX == 0) _startRunTime = 0; return; } _startRunTime = 0; runVFX.Stop(); } } }