So, I’ve posted about this before, but I’ve tried every suggestion I’ve been given with the same issue always arising so I feel like something is just missing from the code at this point. I have a walking sound array being triggered by the arrow keys when I press them down, the sound clips in the array will change depending on which kind of surface (ex. wood, grass, dirt, etc.) my character is on and I have the sounds being changed via OnCollisionColliderHit using tags as a reference point for the change. Everything is being triggered on the right surfaces but the only issue is that the sound is being called every frame-ish. I don’t have anything in the Update function so not sure what’s going on. I’m still quite new to scripting so apologies if I’m making a rookie mistake.
using UnityEngine;
using System.Collections;
public class WormWalk : MonoBehaviour
{
public AudioSource Audio;
public AudioClip[] walkingSounds;
public int pitchRandomness = 1;
// Use this for initialization
void Start ()
{
Audio = (AudioSource)gameObject.GetComponent ("AudioSource");
}
// Update is called once per frame
void Update ()
{
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
Audio.Play();
}
else
{
Audio.Stop ();
}
if (hit.gameObject.tag == "groundFloor")
{
print("Ground");
Audio.clip = walkingSounds[0];
}
if (hit.gameObject.tag == "woodFloor")
{
print("Wood");
Audio.clip = walkingSounds[1];
}
if (hit.gameObject.tag == "stoneFloor")
{
print("Stone");
Audio.clip = walkingSounds[2];
}
if (hit.gameObject.tag == "templeFloor")
{
print("Temple");
Audio.clip = walkingSounds[3];
}
}
}