Untitled
unknown
csharp
2 years ago
1.7 kB
13
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Footstep_Sound : MonoBehaviour
{
[Tooltip("How far gameObject must move until we make sound")]
[SerializeField] private float stepDistance = 0.6f;
[SerializeField] private step[] steps;
private AudioSource audioSource;
private Player_Control pc;
private float currentStep;
private void Start()
{
pc = transform.parent.GetComponent<Player_Control>();
currentStep = stepDistance;
}
private void Update()
{
Calculate();
}
void Calculate()
{
if(pc)
{
if (pc.onGround)
{
if (pc.onMove && !pc.onRun) currentStep -= Time.deltaTime;
else if (pc.onMove && pc.onRun) currentStep -= 1.25f * Time.deltaTime;
}
}
if(currentStep <= 0)
{
if(pc.onGround)
{
CheckAndPlay();
}
currentStep = stepDistance;
}
}
void CheckAndPlay()
{
for (int i = 0; i < steps.Length; i++)
{
if (pc.tagName == steps[i].tagName)
{
int random = Random.Range(0, steps[i].sounds.Length);
audioSource.clip = steps[i].sounds[random];
audioSource.PlayOneShot(audioSource.clip);
return;
}
}
}
}
[System.Serializable]
public class step
{
[Tooltip("Ground's tag for detection")]
public string tagName;
public AudioClip[] sounds;
}
Editor is loading...
Leave a Comment