我需要帮助找到延迟声音的方法

问题描述

我最近一直在搞 Unity 和 VR,我一直在使用 youtube 上的这段代码来开枪 (https://www.youtube.com/watch?v=kD47gUJO7jA) 枪工作正常,但它发出的声音重复得很快而且听起来很糟糕,所以我需要延迟它。我尝试过系统线程,但会暂时停止应用程序并可能导致恶心我也尝试过 task.delay 以多种方式,但它要么不起作用,要么由于错误而无法编译它。有人可以帮我解决这个问题吗?

using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;

public class ControllerInput : MonoBehavIoUr
{
    public AudioClip clip;
    public AudioSource audioSource;
    public Transform gunBarrelTransform;

    // Use this for initialization
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        audioSource.clip = clip;
    }

    // Update is called once per frame
    void Update()
    {
        if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger))
        {
            audioSource.Play();
            RaycastGun();
            
        }
    }

    private void RaycastGun()
    {
        RaycastHit hit;

        if (Physics.Raycast(gunBarrelTransform.position,gunBarrelTransform.forward,out hit))
        {
            if (hit.collider.gameObject.CompareTag("Cube"))
            {
                Destroy(hit.collider.gameObject);
            }
        }
    }
}```

解决方法

问题是您在触发器关闭的每一帧上都调用 export const PersonEdit = props => ( ... <MapContainer center={[51.505,-0.09]} zoom={13} scrollWheelZoom={false} fullWidth> <TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> <Marker position={[51.505,-0.09]}> <Popup> A pretty CSS3 popup. <br /> Easily customizable. </Popup> </Marker> </MapContainer> </SimpleForm> </Edit> ); 。您需要再次测试以查看它是否最近播放过。您可以使用音频剪辑的长度,但您可能希望它更频繁或更不频繁,因此让我们使用任意浮点值。

audioSource.Play