69 lines
2.8 KiB
C#
69 lines
2.8 KiB
C#
using RR.Game2D.Behaviour;
|
|
using RR.Game2D.Core.Managers;
|
|
using UnityEngine;
|
|
using UnityEngine.VFX;
|
|
|
|
[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<Rigidbody2D>();
|
|
playerController = player.GetComponent<PlayerController>();
|
|
|
|
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();
|
|
}
|
|
}
|