Guest User

Untitled

a guest
Aug 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6. [RequireComponent(typeof(AudioSource))]
  7. [RequireComponent(typeof(Collider))]
  8. public class CollisionAudio : MonoBehaviour
  9. {
  10. [SerializeField] private AudioClip collideSoft;
  11. [SerializeField] private AudioClip collideHard;
  12. [SerializeField] private float lowPitchRange = .75F;
  13. [SerializeField] private float highPitchRange = 1.5F;
  14. [SerializeField] private float velocityClipSplit = 10F; // under this volume, use soft sound
  15. // over this volume, use hard sound
  16. private float velToVol = .2F;
  17. private Rigidbody rb;
  18. private AudioSource audioSource;
  19.  
  20. void Start()
  21. {
  22. rb = GetComponent<Rigidbody>();
  23. audioSource = GetComponent<AudioSource>();
  24. }
  25.  
  26. private void Reset()
  27. {
  28. if (!rb) rb = GetComponent<Rigidbody>();
  29. if (!audioSource) audioSource = GetComponent<AudioSource>();
  30. }
  31.  
  32. void OnCollisionEnter(Collision coll)
  33. {
  34. audioSource.pitch = Random.Range(lowPitchRange, highPitchRange);
  35. float hitVol = coll.relativeVelocity.magnitude * velToVol;
  36. if (coll.relativeVelocity.magnitude < velocityClipSplit)
  37. audioSource.PlayOneShot(collideSoft, hitVol);
  38. else
  39. audioSource.PlayOneShot(collideHard, hitVol);
  40. }
  41. }
Add Comment
Please, Sign In to add comment