Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Rigidbody otherRb = collision.rigidbody;
- if (otherRb == null) return;
- Vector3 myVelocity = rb.velocity;
- Vector3 otherVelocity = otherRb.velocity;
- Vector3 relativeVelocity = thisVelocity - otherVelocity;
- float impactForce = relativeVelocity.magnitude;
- float dotProduct = Vector3.Dot(transform.forward, -relativeVelocity.normalized);
- if (dotProduct > 0.5f)
- {
- PlayImpactSoundLerped(impactForce, true);
- }
- else (dotProduct < -0.5f)
- {
- PlayImpactSoundLerped(impactForce, false);
- }
- void PlayImpactSound(float impactForce, bool? frontal)
- {
- float volume = Mathf.Clamp01(impactForce / 20f);
- float pitch = Mathf.Lerp(0.8f, 1.5f, impactForce / 20f);
- if (frontal == true)
- audioSource.PlayOneShot(frontImpactSound, volume);
- else if (frontal == false)
- audioSource.PlayOneShot(rearImpactSound, volume);
- else
- audioSource.PlayOneShot(sideImpactSound, volume);
- audioSource.pitch = pitch;
- }
Advertisement
Add Comment
Please, Sign In to add comment