Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using AC;
- public class FootstepDust : MonoBehaviour
- {
- public ParticleSystem dustParticles;
- private Char acCharacter;
- private float lastMoveSpeed = 0f;
- // Add these configuration parameters
- [Header("Dust Control")]
- [Tooltip("Minimum speed required to show dust")]
- public float minSpeedThreshold = 0.05f;
- [Tooltip("How quickly dust responds to movement changes")]
- public float responsiveness = 0.2f;
- [Tooltip("Base emission rate for dust particles")]
- public float baseEmissionRate = 5f;
- // Track the character's velocity over time
- private Vector3 lastPosition;
- private float currentVelocity;
- void Start()
- {
- acCharacter = GetComponent<Char>();
- if (dustParticles == null)
- dustParticles = GetComponentInChildren<ParticleSystem>();
- if (dustParticles != null) {
- // Configure particle system for more consistent behavior
- var emission = dustParticles.emission;
- emission.rateOverTime = 0;
- dustParticles.Play(); // Keep the system always playing but with zero emission
- }
- lastPosition = transform.position;
- }
- void Update()
- {
- if (acCharacter == null || dustParticles == null)
- return;
- // Calculate actual movement velocity rather than relying only on AC's GetMoveSpeed
- Vector3 currentPosition = transform.position;
- currentVelocity = Vector3.Distance(currentPosition, lastPosition) / Time.deltaTime;
- lastPosition = currentPosition;
- // Combine AC's movement speed with actual measured velocity for more accuracy
- float acSpeed = acCharacter.GetMoveSpeed();
- float effectiveSpeed = Mathf.Max(currentVelocity, acSpeed);
- // Only emit particles when character is moving and on ground
- var emission = dustParticles.emission;
- if (effectiveSpeed > minSpeedThreshold && acCharacter.IsGrounded())
- {
- // Smoothly adjust emission rate rather than abruptly changing it
- float targetRate = effectiveSpeed * baseEmissionRate;
- float currentRate = emission.rateOverTime.constant;
- float newRate = Mathf.Lerp(currentRate, targetRate, responsiveness);
- emission.rateOverTime = newRate;
- }
- else
- {
- // Gradually reduce emission rate to zero instead of stopping immediately
- float currentRate = emission.rateOverTime.constant;
- if (currentRate > 0) {
- emission.rateOverTime = Mathf.Lerp(currentRate, 0, responsiveness * 2);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment