Skip to content

Instantly share code, notes, and snippets.

@KaganAyten
Created February 23, 2024 05:06
Show Gist options
  • Save KaganAyten/e09305cfbc1cc6792ecdc619c079e1f4 to your computer and use it in GitHub Desktop.
Save KaganAyten/e09305cfbc1cc6792ecdc619c079e1f4 to your computer and use it in GitHub Desktop.
FootstepSFXController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FootstepController : MonoBehaviour
{
private AudioSource aSource;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private AudioClip walk, sandWalk, mudWalk,grassWalk,metalWalk;
private RaycastHit hit;
private void Awake()
{
aSource = GetComponent<AudioSource>();
}
public void DoSFX()
{
aSource.pitch = Random.Range(0.85f, 1.2f);
Ray ray = new Ray(transform.position+transform.up*0.5f, transform.up * -1);
if(Physics.Raycast(ray,out hit, 1, groundLayer))
{
print(hit.collider.name);
switch (hit.collider.tag)
{
case "Mud":
aSource.PlayOneShot(mudWalk); break;
case "Sand":
aSource.PlayOneShot(sandWalk); break;
case "Grass":
aSource.PlayOneShot(grassWalk); break;
case "Metal":
aSource.PlayOneShot(metalWalk); break;
default: aSource.PlayOneShot(footstepSFX); break;
}
}
}
//yollanılan ışını sahnede görmek için
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position + transform.up * 0.5f, transform.position + transform.up * 0.5f + transform.up * -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment