- Home /
Help with sound playing when a certain bool = false
I am trying to make the footstep sound play when the player's speed = 3f
(3f is sprinting speed) . For that though i have to check whether or not the player is sprinting by calling it from the Update()
function.
Now if i call it from there? It'l run every frame which will not give the sound enough time to finish playing. So i created a bool: playedSoundRun
and put it in an if
statement
if (playedSoundRun == false)
{
GrassRunSound();
}
if set to false
it will continue to check the GrassRunSound()
statement every frame. I also created another bool soundRunCheck
to check if the speed = 3f
The sound works when i'm running, the problem is just that when the speed goes back to 1, the GrassRunSound()
still continues to play. If someone could help me figure out whats wrong with my code that would be amazing.
Here's my whole script (Ignore the GrassWalkSound()
function for now:
public CharacterController charController;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
private Vector3 velocity;
private bool isGrounded;
public AudioSource grassWalk;
public AudioSource grassRun;
private bool playedSoundRun = false;
private bool soundRunCheck = false;
void Start()
{
playedSoundRun = grassRun.isPlaying;
grassWalk.Stop();
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
charController.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
charController.Move(velocity * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
if (playedSoundRun == false)
{
GrassRunSound();
}
CharacterSprint();
}
private void CharacterSprint()
{
if (isGrounded && Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKey(KeyCode.Z))
{
soundRunCheck = true;
speed = speed + 2f;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
soundRunCheck = false;
speed = 1f;
}
if (Input.GetKey(KeyCode.LeftShift) == false && Input.GetKey(KeyCode.Z))
{
soundRunCheck = false;
speed = 1f;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Water"))
{
gravity = gravity - 10f;
speed = speed - 1f;
}
if (collision.gameObject.tag == "Water")
{
gravity = -17;
speed = 1f;
}
}
void GrassWalkSound()
{
}
void GrassRunSound()
{
if (isGrounded && soundRunCheck == true)
{
grassRun.Play();
grassRun.loop = true;
playedSoundRun = true;
}
if (soundRunCheck == false)
{
grassRun.Stop();
playedSoundRun = false;
}
}
}
PS: I know all the if statements makes it seem a little noob-ish, i'm a beginner
Answer by rh_galaxy · Aug 19, 2020 at 12:32 AM
GrassRunSound() is never called again when playedSoundRun is set to true, so grassRun.Stop() will never be called...
if (soundRunCheck == false)
{
grassRun.Stop();
playedSoundRun = false;
}
needs to run before CharacterSprint(), I think. Move that block from GrassRunSound.
Your answer
Follow this Question
Related Questions
Switch statement not running on every frame 1 Answer
How to call PlayerPrefs in an Update function without hogging memory? 0 Answers
refactoring functions which *must* be in Update() 0 Answers
update method isnt working 0 Answers
How do I update an image to all connected Players on a Multiplayer server? 0 Answers