Little Trafalgar dust trail fun
This commit is contained in:
91
Assets/Scripts/FX/TrafalgarFXController.cs
Normal file
91
Assets/Scripts/FX/TrafalgarFXController.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using UnityEngine;
|
||||
using Input;
|
||||
|
||||
namespace FX
|
||||
{
|
||||
/// <summary>
|
||||
/// Controls particle effects based on player movement.
|
||||
/// Automatically enables/disables particle emission when the player starts/stops moving.
|
||||
/// Attach this to the same GameObject as the PlayerTouchController.
|
||||
/// </summary>
|
||||
public class TrafalgarFXController : MonoBehaviour
|
||||
{
|
||||
private PlayerTouchController _playerController;
|
||||
private ParticleSystem _particleSystem;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Get the PlayerTouchController on this game object
|
||||
_playerController = GetComponent<PlayerTouchController>();
|
||||
|
||||
if (_playerController == null)
|
||||
{
|
||||
Debug.LogError("TrafalgarFXController: No PlayerTouchController found on this GameObject! This component should be attached to the same GameObject as PlayerTouchController.");
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the particle system in children
|
||||
_particleSystem = GetComponentInChildren<ParticleSystem>();
|
||||
|
||||
if (_particleSystem == null)
|
||||
{
|
||||
Debug.LogError("TrafalgarFXController: No ParticleSystem found in children! This component requires a ParticleSystem in the hierarchy below it.");
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure emission is disabled at start
|
||||
var emission = _particleSystem.emission;
|
||||
emission.enabled = false;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Subscribe to movement events
|
||||
_playerController.OnMovementStarted += EnableParticles;
|
||||
_playerController.OnMovementStopped += DisableParticles;
|
||||
|
||||
// Set initial state based on current movement status
|
||||
SetParticlesState(_playerController.IsMoving);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
// Unsubscribe from events when this component is destroyed
|
||||
if (_playerController != null)
|
||||
{
|
||||
_playerController.OnMovementStarted -= EnableParticles;
|
||||
_playerController.OnMovementStopped -= DisableParticles;
|
||||
}
|
||||
}
|
||||
|
||||
private void EnableParticles()
|
||||
{
|
||||
SetParticlesState(true);
|
||||
}
|
||||
|
||||
private void DisableParticles()
|
||||
{
|
||||
SetParticlesState(false);
|
||||
}
|
||||
|
||||
private void SetParticlesState(bool enabled)
|
||||
{
|
||||
if (_particleSystem != null)
|
||||
{
|
||||
var emission = _particleSystem.emission;
|
||||
emission.enabled = enabled;
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
// Optionally play the system when enabling (useful if it's not in looping mode)
|
||||
if (!_particleSystem.isPlaying)
|
||||
{
|
||||
_particleSystem.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/FX/TrafalgarFXController.cs.meta
Normal file
3
Assets/Scripts/FX/TrafalgarFXController.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5435358d90b4c29982a670998cd9a56
|
||||
timeCreated: 1758617481
|
||||
Reference in New Issue
Block a user