Почему аудиоклип не слышен при воспроизведении? - PullRequest
0 голосов
/ 09 марта 2020
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Whilefun.FPEKit;

public class RoboSphereWindowBreakInteraction : MonoBehaviour
{
    public Transform target;
    public InteractableObjects interactableObjects;
    public AudioClip audioClip;
    public float speed;

    private bool hasStarted = false;
    private Animator anim;

    void Update()
    {
        if ((Input.GetKeyDown(KeyCode.B) || (hasStarted == true)))
        {
            float step = speed * Time.deltaTime; // calculate distance to move
            transform.position = Vector3.MoveTowards(transform.position, target.position, step);

            hasStarted = true;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "Square 1")
        {
            GetComponent<Rigidbody>().isKinematic = false;
            hasStarted = false;
            Destroy(GameObject.Find("Wall_Window_Long_03"));
        }
    }

    public void ActivateRoboSphere()
    {
        foreach(Transform child in transform)
        {
            if(child.name == "Camera")
            {
                RepositionCamera(child);
            }
        }

        anim = GetComponent<Animator>();
        anim.enabled = true;

        if (!anim.GetBool("Open_Anim"))
        {
            anim.SetBool("Open_Anim", true);

            StartCoroutine(PlayAudio());
        }
        else
        {
            //anim.SetBool("Open_Anim", false);
        }
    }

    private void RepositionCamera(Transform camera)
    {
        var Eyes = GameObject.Find("eyeDome");

        camera.position = Eyes.transform.position + Eyes.transform.forward;
        camera.LookAt(Eyes.transform);
        camera.GetComponent<Camera>().enabled = true;
    }

    IEnumerator PlayAudio()
    {
        AudioSource audio = GetComponent<AudioSource>();

        audio.Play();
        yield return new WaitForSeconds(audio.clip.length);
        audio.clip = audioClip;
        audio.Play();
    }
}

Он попадает внутрь PlayAudio, но звук не слышен.

Настройки снимка экрана компонента аудиоисточника:

Audio Source

1 Ответ

1 голос
/ 09 марта 2020

Единственная проблема, которую я вижу с этим кодом, состоит в том, что изначально клип вашего аудиоисточника не установлен ни к чему. Таким образом, в этой строке возникнет исключение пустого ref:

 yield return new WaitForSeconds(audio.clip.length);

Добавьте пустую проверку, и вы должны быть хороши до go

 if (audioSource.clip != null)
 {
     yield return new WaitForSeconds(audioSource.clip.length);    
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...