So what Im trying to do is, when my player holds left mouse button, it charges a shot, and while charging the game should play the charging shot sound.
But whats happening: the sound starts playing after I release the left button (as if it was written inside the GetMouseButtonUp). Heres my script:
if (Input.GetMouseButton(0)) {
musicPlayer.PlaySound("Charging");
}
if (Input.GetMouseButtonUp(0)) {
Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = (Input.mousePosition - sp).normalized;
musicPlayer.StopSound("Charging");
Side Note: If I assign the stop sound command to the GetMouseUp (like the script is showing) , the sound doesnt play at all, most likely because the stop sound command is called right after the play sound is called wrongly afte releasing left mouse.
If someone could help me figure whats wrong, I appreciate it, thanks in advance.
Try Input.GetMouseButtonDown(0) so it starts playing the sound only on the frame you first click down
Try something like this
if(Input.GetMouseButtonDown(0) && myMusic.isPlaying == false)
myMusic.Play();
If you press left mouse AND the audio source doesn’t play anything, the audio source will play the selected clip.
Hope that helped, good luck.