Files
2025-05-29 22:31:40 +03:00

227 lines
9.2 KiB
C#

using UnityEngine;
public class FootSteps : MonoBehaviour
{
[SerializeField] private Animator _animator;
[SerializeField] private Transform RaycastTransform;
[SerializeField] private AudioClip[] _clipsGrass;
[SerializeField] private AudioClip[] _clipsDirt;
[SerializeField] private AudioClip[] _clipsLeaves;
[SerializeField] private AudioClip[] _clipsConcrete;
[SerializeField] private AudioClip[] _clipsWood;
private AudioSource _audioSource;
private CheckTerrainTexture checkTerrainTexture;
RaycastHit hit;
Ray ray;
[SerializeField] private float raydistance = 0.05f; //The distance of the ray.
[SerializeField]private GameObject _currentHitObject;
private void Awake()
{
_audioSource = GetComponent<AudioSource>();
if (_animator == null)
{
_animator = GetComponent<Animator>();
}
checkTerrainTexture = GetComponent<CheckTerrainTexture>();
}
private void Update()
{
Debug.DrawRay(ray.origin, -RaycastTransform.up * raydistance, Color.green);
ray = new Ray(RaycastTransform.position, -RaycastTransform.up); //The ray that comes from the camera.
}
void WalkingStep()
{
}
private void Step() //The method executes when the player/Npc walks.
{
//#region Terrain Step Clips
//checkTerrainTexture.GetTerrainTexture(); //Checks the texture of the ground.
//#region Grass
//if (checkTerrainTexture.textureValues[1] > 0 || checkTerrainTexture.textureValues[2] > 0 || checkTerrainTexture.textureValues[3] > 0
// || checkTerrainTexture.textureValues[4] > 0 || checkTerrainTexture.textureValues[9] > 0)
//{
// AudioClip clip = GetRandomClipGrass(); //Calls the method that gets a random clip sound from the array.
// if (_animator.GetFloat("speedPercent") > 0.1 && _animator.GetFloat("speedPercent") < 0.6 || _animator.GetFloat("Forward") > 0.1 && _animator.GetFloat("Forward") < 0.6)
// {
// _audioSource.PlayOneShot(clip, volumeScale: 0.035f);
// }
//}
//#endregion
//#region Leaves
//if (checkTerrainTexture.textureValues[5] > 0 || checkTerrainTexture.textureValues[6] > 0)
//{
// AudioClip clip = GetRandomClipLeaves(); //Calls the method that gets a random clip sound from the array.
// if (_animator.GetFloat("speedPercent") > 0.1 && _animator.GetFloat("speedPercent") < 0.6 || _animator.GetFloat("Forward") > 0.1 && _animator.GetFloat("Forward") < 0.6)
// {
// _audioSource.PlayOneShot(clip, volumeScale: 0.007f);
// }
//}
//#endregion
//#region Dirt
//if (checkTerrainTexture.textureValues[0] > 0 || checkTerrainTexture.textureValues[7] > 0)
//{
// AudioClip clip = GetRandomeClipDirt(); //Calls the method that gets a random clip sound from the array.
// if (_animator.GetFloat("speedPercent") > 0.1 && _animator.GetFloat("speedPercent") < 0.6 || _animator.GetFloat("Forward") > 0.1 && _animator.GetFloat("Forward") < 0.6)
// {
// _audioSource.PlayOneShot(clip, volumeScale: 0.020f);
// }
//}
//#endregion
//#endregion
#region Not Terrain Steps
if (_animator.GetFloat("speedPercent") > 0.1 && _animator.GetFloat("speedPercent") < 0.6 || _animator.GetFloat("Forward") > 0.1 && _animator.GetFloat("Forward") < 0.6)
{
AudioClip clip = GetRandomeClipDirt(); //Calls the method that gets a random clip sound from the array.
AudioClip clipConcrete = GetRandomClipConcrete(); //Calls the method that gets a random clip sound from the array.
AudioClip clipWood = GetRandomClipWood(); //Calls the method that gets a random clip sound from the array.
if (Physics.Raycast(ray, out hit, raydistance))
{
_currentHitObject = hit.transform.gameObject;
print("FootstepXD");
if (hit.collider.tag == "ConcreteSurface")
{
_audioSource.PlayOneShot(clipConcrete, volumeScale: 0.5f);
print("FootstepConcrete");
//play concrete sound code
}
else if (hit.collider.tag == "DirtSurface")
{
_audioSource.PlayOneShot(clip, volumeScale: 0.5f);
print("Footstep");
//play concrete sound code
}
else if (hit.collider.tag == "Water")
{
//play Water sound code
}
else if (hit.collider.tag == "WoodSurface")
{
_audioSource.PlayOneShot(clipWood, volumeScale: 0.5f);
print("clipWood");
//play Water sound code
}
}
}
#endregion
}
public void StepRun() //The method executes when the player/Npc runs.
{
#region Terrain
//checkTerrainTexture.GetTerrainTexture(); //Checks the texture of the ground.
//#region Grass
//if (checkTerrainTexture.textureValues[1] > 0 || checkTerrainTexture.textureValues[2] > 0 || checkTerrainTexture.textureValues[3] > 0
// || checkTerrainTexture.textureValues[4] > 0 || checkTerrainTexture.textureValues[9] > 0)
//{
// AudioClip clip = GetRandomClipGrass(); //Calls the method that gets a random clip sound from the array.
// if (_animator.GetFloat("speedPercent") >= 0.6 || _animator.GetFloat("Forward") >= 0.6)
// {
// _audioSource.PlayOneShot(clip, volumeScale: 0.035f);
// }
//}
//#endregion
//#region Leaves
//if (checkTerrainTexture.textureValues[5] > 0 || checkTerrainTexture.textureValues[6] > 0)
//{
// AudioClip clip = GetRandomClipLeaves(); //Calls the method that gets a random clip sound from the array.
// if (_animator.GetFloat("speedPercent") >= 0.6 || _animator.GetFloat("Forward") >= 0.6)
// {
// _audioSource.PlayOneShot(clip, volumeScale: 0.007f);
// }
//}
//#endregion
//#region Dirt
//if (checkTerrainTexture.textureValues[0] > 0 || checkTerrainTexture.textureValues[7] > 0)
//{
// AudioClip clip = GetRandomeClipDirt(); //Calls the method that gets a random clip sound from the array.
// if (_animator.GetFloat("speedPercent") >= 0.6 || _animator.GetFloat("Forward") >= 0.6)
// {
// _audioSource.PlayOneShot(clip, volumeScale: 0.020f);
// }
//}
//#endregion
#endregion
#region Not Terrain Steps
if (_animator.GetFloat("speedPercent") >= 0.6 || _animator.GetFloat("Forward") >= 0.6)
{
AudioClip clip = GetRandomeClipDirt(); //Calls the method that gets a random clip sound from the array.
AudioClip clipConcrete = GetRandomClipConcrete(); //Calls the method that gets a random clip sound from the array.
AudioClip clipWood = GetRandomClipWood(); //Calls the method that gets a random clip sound from the array.
if (Physics.Raycast(ray, out hit, raydistance))
{
_currentHitObject = hit.transform.gameObject;
print("FootstepXD");
if (hit.collider.tag == "ConcreteSurface")
{
_audioSource.PlayOneShot(clipConcrete, volumeScale: 0.5f);
print("FootstepConcrete");
//play concrete sound code
}
else if (hit.collider.tag == "DirtSurface")
{
_audioSource.PlayOneShot(clip, volumeScale: 0.5f);
print("Footstep");
//play concrete sound code
}
else if (hit.collider.tag == "Water")
{
//play Water sound code
}
else if (hit.collider.tag == "WoodSurface")
{
_audioSource.PlayOneShot(clipWood, volumeScale: 0.5f);
print("clipWood");
//play Water sound code
}
}
}
#endregion
}
private AudioClip GetRandomClipConcrete() //Gets a random clip sound from the array.
{
return _clipsConcrete[UnityEngine.Random.Range(0, _clipsConcrete.Length)];
}
private AudioClip GetRandomClipGrass() //Gets a random clip sound from the array.
{
return _clipsGrass[UnityEngine.Random.Range(0, _clipsGrass.Length)];
}
private AudioClip GetRandomeClipDirt() //Gets a random clip sound from the array.
{
return _clipsDirt[UnityEngine.Random.Range(0, _clipsDirt.Length)];
}
private AudioClip GetRandomClipLeaves() //Gets a random clip sound from the array.
{
return _clipsLeaves[UnityEngine.Random.Range(0, _clipsLeaves.Length)];
}
private AudioClip GetRandomClipWood() //Gets a random clip sound from the array.
{
return _clipsWood[UnityEngine.Random.Range(0, _clipsWood.Length)];
}
}