Unity: 2022.1.24f1
Adventure Creator: 1.76.3
Hey all,
I’ve been having a slight problem hearing footsteps with the AutoSetFootstepSounds script on the wiki. I've been following this post https://adventurecreator.org/forum/discussion/11596/change-footstep-sounds-per-physic-material and added the debug code to the script. The log is spammed with No collider found UnityEngine.Debug:Log (object)
regardless of which collider I stand on.
I have played around with the Ray length but it has no effect. if I assign audio clips to the Footsteps sound component I do hear the correct audio clips as expected.
I have the script setup as it mentions on the wiki page:
Here is my AutoSetFootstepSounds.cs:
using UnityEngine;
using AC;
public class AutoSetFootstepSounds : MonoBehaviour
{
[SerializeField] private LayerMask layerMask = new LayerMask();
[SerializeField] private float rayLength = 0.2f;
[SerializeField] private FootstepMaterial[] footstepMaterials = new FootstepMaterial[0];
private Char character;
private FootstepSounds footstepSounds;
private PhysicMaterial standingOnMaterial;
private void Awake()
{
character = GetComponent<Char>();
footstepSounds = GetComponentInChildren<FootstepSounds>();
}
private void LateUpdate()
{
if (character.IsGrounded())
{
Vector3 rayStart = transform.position + (0.5f * rayLength * Vector3.up);
Debug.DrawRay(rayStart, Vector3.up * -rayLength);
if (Physics.Raycast(rayStart, Vector3.down, out RaycastHit hitInfo, rayLength, layerMask))
{
Debug.Log("Collider: " + hitInfo.collider + ", Current material: " + standingOnMaterial);
if (hitInfo.collider.material && standingOnMaterial != hitInfo.collider.material)
{
standingOnMaterial = hitInfo.collider.material;
OnStandMaterial(standingOnMaterial);
}
}
else Debug.Log("No collider found");
}
}
private void OnStandMaterial(PhysicMaterial material)
{
foreach (FootstepMaterial footstepMaterial in footstepMaterials)
{
if (footstepMaterial.Material == material)
{
footstepMaterial.Apply(footstepSounds);
return;
}
}
}
[System.Serializable]
private class FootstepMaterial
{
[SerializeField] private PhysicMaterial material = null;
[SerializeField] private AudioClip[] walkSounds = new AudioClip[0];
[SerializeField] private AudioClip[] runSounds = new AudioClip[0];
public void Apply(FootstepSounds footstepSounds)
{
if (walkSounds.Length > 0)
{
footstepSounds.footstepSounds = walkSounds;
}
if (runSounds.Length > 0)
{
footstepSounds.runSounds = runSounds;
}
if (!footstepSounds.character.IsGrounded() && walkSounds.Length > 0)
{
footstepSounds.soundToPlayFrom.audioSource.clip = walkSounds[0];
}
}
public PhysicMaterial Material { get { return material; } }
}
}
Just wondering If I have the components setup correctly?
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
I can't see any obvious issue with your screenshots. Are your floor colliders placed on the FootSteps layer, as set in your LayerMask field?
Morning Chris,
Thanks, yeah the floor colliders are assigned to the FootSteps layer. Interestingly the debug is working now and reporting the correct materials as I walk over them.
I made a quick video to demonstrate.
If the Logs are reporting correct now, try moving them into the script's Apply function, which should be called each time the surface material changes.
This function iterates through the Inspectors "Footstep Materials" array and applies the sounds when a match is found. Is the function being called, and is it finding a match?
Cheers Chris, I added the debug code to the Apply function but ran into a bunch of errors. I'm not much of a scripter so I probably, no defiantly, did something incorrect.
What debug code did you add, and what were the errors?