How do I make a button play a sound while being pressed and have it stop while NOT being pressed?
This is my current script for it.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class PlaySound : MonoBehaviour
{
public AudioSource soundPlayer;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void playThisSoundEffect()
{
soundPlayer.Play();
}
}
There are various forms, but some of the easiest involve OnKeyDown and OnKeyUp if its via code. Or if it is a UI button, you could create an event trigger and then basically do the same thing: PointerUp or PointerDown