Eonirr

Untitled

Mar 28th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. Rigidbody otherRb = collision.rigidbody;
  2. if (otherRb == null) return;
  3.  
  4. Vector3 myVelocity = rb.velocity;
  5. Vector3 otherVelocity = otherRb.velocity;
  6.  
  7. Vector3 relativeVelocity = thisVelocity - otherVelocity;
  8.  
  9. float impactForce = relativeVelocity.magnitude;
  10.  
  11. float dotProduct = Vector3.Dot(transform.forward, -relativeVelocity.normalized);
  12.  
  13. if (dotProduct > 0.5f)
  14. {
  15. PlayImpactSoundLerped(impactForce, true);
  16. }
  17. else (dotProduct < -0.5f)
  18. {
  19. PlayImpactSoundLerped(impactForce, false);
  20. }
  21.  
  22.  
  23. void PlayImpactSound(float impactForce, bool? frontal)
  24. {
  25. float volume = Mathf.Clamp01(impactForce / 20f);
  26. float pitch = Mathf.Lerp(0.8f, 1.5f, impactForce / 20f);
  27.  
  28. if (frontal == true)
  29. audioSource.PlayOneShot(frontImpactSound, volume);
  30. else if (frontal == false)
  31. audioSource.PlayOneShot(rearImpactSound, volume);
  32. else
  33. audioSource.PlayOneShot(sideImpactSound, volume);
  34.  
  35. audioSource.pitch = pitch;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment