Advertisement
noradninja

Footstep

Oct 16th, 2023
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1.  
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class SFXController : MonoBehaviour {
  7.  
  8.     public AudioSource audioSource;
  9.     public AudioClip audioClip;
  10.     public AudioClip[] grassSounds;
  11.     public AudioClip[] roadSounds;
  12.     public AudioClip[] wetRoadSounds;
  13.     public AudioClip[] currentClips;
  14.     public float distance = 0.055f;
  15.     private RaycastHit hit;
  16.     private int i;
  17.     private int tick;
  18.     private int maskValue;
  19.  
  20.     private void Update()
  21.     {
  22.         if (tick == 5)
  23.         {
  24.             every5FrameUpdate();//TODO: write this so its a subscriber fired event so we can ditch Update()
  25.             tick = 0;
  26.         }
  27.         else tick++;
  28.     }
  29.     private void every5FrameUpdate(){
  30.  
  31.         if (Physics.Raycast(transform.position, Vector3.down, out hit, distance,
  32.                 LayerMask.GetMask("Ground"))){
  33.             currentClips = grassSounds;
  34.         }
  35.         else if (Physics.Raycast(transform.position, Vector3.down, out hit, distance,
  36.                              LayerMask.GetMask("Water_Decals")))
  37.         {
  38.                     //print("Water_Puddle");
  39.                     currentClips = wetRoadSounds;
  40.         }
  41.         else if (Physics.Raycast(transform.position, Vector3.down, out hit, distance,
  42.                      LayerMask.GetMask("Road")))
  43.         {
  44.             currentClips = roadSounds;
  45.         }
  46.        
  47.  
  48.     }
  49.  
  50.     private void footstepSound(){
  51.         i = Random.Range(0, currentClips.Length);
  52.         audioClip = currentClips[i];
  53.         audioSource.pitch = Random.Range (0.75f, 1.25f);
  54.         audioSource.PlayOneShot(audioClip);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement