Mike
Mike

Reputation: 21

How to play the right sound when selected on a object in unity/c#

I tried by making a script to when I tap on a object on my scene to make sound. So, I have audio source and my script implemented on the object. However, with my script below it only deploys the first audio that i have which is (cup). What I want to change in my code or make it better is dont let audio run until I click on the object. and any way to do where I click on a object the audio triggers? please advise. thanks for the help.

Update: Right now, if I select everywhere the sound triggers and whenever I click on other object it gives the one audio of all of my object, that is not what I wanted.

here is my code:

    public class TextToSpeech : MonoBehaviour
{

    [SerializeField] AudioClip _audioClip;
    [SerializeField] [Range(0.0f, 1.0f)] float _volume = 1;
    AudioSource _audioSource;


    public AudioClip SoundToPlay;
    public float Volume;
    public bool alreadyPlayed = false;
    public bool playOnAwake = false;


    private Touch touch;
    private Vector2 beginTouchPosition, endTouchPosition;




    private bool isPlaying;


    void Start()
    {
        _audioSource = GetComponent<AudioSource>();
        _audioSource.clip = _audioClip;
        _audioSource.volume = _volume;


    }


    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            _audioSource.enabled = true;
            if (!_audioSource.isPlaying) {
                _audioSource.clip = SoundToPlay;
                _audioSource.Play ();
            }
            else
            {
                _audioSource.enabled = false;

            }


        }


    }

}

Upvotes: 0

Views: 1286

Answers (2)

VPavliashvili
VPavliashvili

Reputation: 655

So, After realizing you problems I changed this answer.

At first your code must be attached only one gameobject, for example camera, like thislike this

also I named my script StackOverflow, you can name it whatever you want.

And BoxCollider must be attached to your plastic bottle or cup, like thisenter image description here you can set it to trigger or nontrigger(depended on your necessity) and set its size to fit the real size of your objects in the scene.

and at last this is the Script which I named StackOverFlow, and SpeechToText in your case

public class StackOverFlow : MonoBehaviour {

[SerializeField] AudioClip _audioClip;
[SerializeField] [Range(0.0f, 1.0f)] float _volume = 1;
AudioSource _audioSource;
bool isPlaying;

void Start() {
    _audioSource = GetComponent<AudioSource>();
    _audioSource.clip = _audioClip;
    _audioSource.volume = _volume;
}

void Update() {

    if (Input.touchCount > 0) {

        Touch touch = Input.touches[0];
        if (touch.phase == TouchPhase.Began) {

            Ray touchRay = Camera.main.ScreenPointToRay(touch.position);
            Physics.Raycast(touchRay, out RaycastHit hit, Mathf.Infinity);

            if (hit.collider != null) {
                if (hit.collider.name.Contains("Cup") || hit.collider.name.Contains("Bottle")) {
                    PlaySound();
                }
                else {
                    Debug.Log($"otherobject {hit.collider.name}");
                }
            }

        }
    }
}

private void PlaySound() {
    if (!isPlaying) {
        _audioSource.Play();
        isPlaying = true;
        StartCoroutine(CheckIfPlaying());
    }
}

private IEnumerator CheckIfPlaying() {
    yield return new WaitForSeconds(_audioClip.length);
    isPlaying = false;
}

}

ask me in comments if something is unclear

Upvotes: 1

Jay
Jay

Reputation: 2946

So you seem to be misunderstanding the use of OnTriggerEnter, you would be better raycasting from the touch position and playing based on that

void Update()
{
    Camera mainCam = Camera.main;  
    for (int i = 0; i < Input.touchCount; ++i)
    {
        Vector3 touchPos = mainCam.ScreenToWorldPoint(Input.GetTouch(i).position);
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.forward);
        if(hit.gameObject == this.gameObject){
            // play sound
        }
    }
}

Upvotes: 0

Related Questions