Hello friends, I am making a game where the player is a ball that rolls around. I have a problem making a script that makes a sound when the ball is rolling. Here is my script.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class PlayRoll : MonoBehaviour {
private bool grounded;
void Start() {
grounded = false;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("ground"))
{
grounded = true;
}
else
{
grounded = false;
}
playSoundWhenGrounded ();
}
void playSoundWhenGrounded() {
AudioSource audio = GetComponent<AudioSource>();
if (grounded == true) {
audio.Play();
}
}
}
also, if u know a way to make that sound only when the ball is gaining speed, so that it wont play this sound when is grounded but the ball is not moving, don’t hesitate to tell me :). Thank you!