ImpactSoundPlayer.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDKBase;
  4. using VRC.Udon;
  5. using System;
  6. [DisallowMultipleComponent]
  7. [AddComponentMenu("Artyom Scripting System/Impact Sound Player")]
  8. public class ImpactSoundPlayer : UdonSharpBehaviour
  9. {
  10. public GameObject smallImpactSoundObject; // GameObject for small impact sound
  11. public GameObject mediumImpactSoundObject; // GameObject for medium impact sound
  12. public GameObject largeImpactSoundObject; // GameObject for large impact sound
  13. public float mediumImpactThreshold = 5.0f; // Threshold for medium impact
  14. public float largeImpactThreshold = 10.0f; // Threshold for large impact
  15. private void OnCollisionEnter(Collision collision)
  16. {
  17. float impactForce = collision.relativeVelocity.magnitude;
  18. if (impactForce >= largeImpactThreshold)
  19. {
  20. PlaySound(largeImpactSoundObject);
  21. }
  22. else if (impactForce >= mediumImpactThreshold)
  23. {
  24. PlaySound(mediumImpactSoundObject);
  25. }
  26. else
  27. {
  28. PlaySound(smallImpactSoundObject);
  29. }
  30. }
  31. private void PlaySound(GameObject soundObject)
  32. {
  33. if (soundObject != null)
  34. {
  35. soundObject.SetActive(false); // Reset the sound object
  36. soundObject.SetActive(true); // Activate the sound object to play the sound
  37. }
  38. }
  39. }