mikdog

Claude script

Mar 8th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. using UnityEngine;
  2. using AC;
  3.  
  4. public class FootstepDust : MonoBehaviour
  5. {
  6. public ParticleSystem dustParticles;
  7. private Char acCharacter;
  8. private float lastMoveSpeed = 0f;
  9.  
  10. // Add these configuration parameters
  11. [Header("Dust Control")]
  12. [Tooltip("Minimum speed required to show dust")]
  13. public float minSpeedThreshold = 0.05f;
  14. [Tooltip("How quickly dust responds to movement changes")]
  15. public float responsiveness = 0.2f;
  16. [Tooltip("Base emission rate for dust particles")]
  17. public float baseEmissionRate = 5f;
  18.  
  19. // Track the character's velocity over time
  20. private Vector3 lastPosition;
  21. private float currentVelocity;
  22.  
  23. void Start()
  24. {
  25. acCharacter = GetComponent<Char>();
  26.  
  27. if (dustParticles == null)
  28. dustParticles = GetComponentInChildren<ParticleSystem>();
  29.  
  30. if (dustParticles != null) {
  31. // Configure particle system for more consistent behavior
  32. var emission = dustParticles.emission;
  33. emission.rateOverTime = 0;
  34. dustParticles.Play(); // Keep the system always playing but with zero emission
  35. }
  36.  
  37. lastPosition = transform.position;
  38. }
  39.  
  40. void Update()
  41. {
  42. if (acCharacter == null || dustParticles == null)
  43. return;
  44.  
  45. // Calculate actual movement velocity rather than relying only on AC's GetMoveSpeed
  46. Vector3 currentPosition = transform.position;
  47. currentVelocity = Vector3.Distance(currentPosition, lastPosition) / Time.deltaTime;
  48. lastPosition = currentPosition;
  49.  
  50. // Combine AC's movement speed with actual measured velocity for more accuracy
  51. float acSpeed = acCharacter.GetMoveSpeed();
  52. float effectiveSpeed = Mathf.Max(currentVelocity, acSpeed);
  53.  
  54. // Only emit particles when character is moving and on ground
  55. var emission = dustParticles.emission;
  56.  
  57. if (effectiveSpeed > minSpeedThreshold && acCharacter.IsGrounded())
  58. {
  59. // Smoothly adjust emission rate rather than abruptly changing it
  60. float targetRate = effectiveSpeed * baseEmissionRate;
  61. float currentRate = emission.rateOverTime.constant;
  62. float newRate = Mathf.Lerp(currentRate, targetRate, responsiveness);
  63.  
  64. emission.rateOverTime = newRate;
  65. }
  66. else
  67. {
  68. // Gradually reduce emission rate to zero instead of stopping immediately
  69. float currentRate = emission.rateOverTime.constant;
  70. if (currentRate > 0) {
  71. emission.rateOverTime = Mathf.Lerp(currentRate, 0, responsiveness * 2);
  72. }
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment