【问题标题】:Playing a sound when the object gets through a specific rotation当对象经过特定旋转时播放声音
【发布时间】:2020-01-16 20:19:56
【问题描述】:

每当对象旋转通过某个点时,我都会尝试播放声音。代码工作正常,但突然停止了,我不知道还能做什么。

对象是一扇门,根据 Unity 的变换信息,它沿其 Z 轴从 -180 旋转到 -300。我希望在门 transform.rotation.z 小于 -190 时播放声音“portaFechando”,但它不起作用。

我只能听到“portaAbrindo”的声音。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class abrirPorta : MonoBehaviour
{
    Animator anim;
    bool portaFechada = true;
    public AudioSource audio;
    public AudioClip abrindo;
    public AudioClip fechando;




    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();


    }

    // Update is called once per frame
    void Update()
    {


        // checkando input para abrir a porta
        if (Input.GetKeyDown("space") && portaFechada == true)
        {
            anim.SetBool("portaFechada", false);
            anim.SetFloat("portaSpeed", 1);
            portaFechada = false;
            audio.clip = abrindo;
            audio.Play();

        }

        // checkando input para fechar porta
        else if (Input.GetKeyDown("space") && portaFechada == false)
        {
            anim.SetBool("portaFechada", true);
            anim.SetFloat("portaSpeed", -1);
            portaFechada = true;
         }

        // tocando som de fechando checkando rotação (bugou)
        if (portafechada == false && transform.rotation.z <= -190)
        {
            Debug.Log("Worked!");
            audio.clip = fechando;
            audio.Play();

        }



    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    目前,您正在访问四元数的 z 分量,它不是围绕 z 轴的角度的度量。

    请参考transform.eulerAngles.z,它是一个介于 0 和 360 之间的值。这里,-190 相当于 170,-300 相当于 60,因此,您可以检查 transform.eulerAngles.z 是否小于或等于170.

    我还建议跟踪自按下关门按钮后是否已经播放了关门声音。另外,不要只在portafechada 为假时播放声音,而是只在它为真时播放:

    Animator anim;
    bool portaFechada = true;
    public AudioSource audio;
    public AudioClip abrindo;
    public AudioClip fechando;
    
    private bool playedSoundAlready = true;
    
    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }
    
    // Update is called once per frame
    void Update()
    {
        // checkando input para abrir a porta
        if (Input.GetKeyDown("space") && portaFechada)
        {
            anim.SetBool("portaFechada", false);
            anim.SetFloat("portaSpeed", 1);
            portaFechada = false;
            audio.clip = abrindo;
            audio.Play();
        }
    
        // checkando input para fechar porta
        else if (Input.GetKeyDown("space") && !portaFechada)
        {
            anim.SetBool("portaFechada", true);
            anim.SetFloat("portaSpeed", -1);
            portaFechada = true;
            playedSoundAlready = false;
         }
    
        // tocando som de fechando checkando rotação (bugou)
        if (!playedSoundAlready && portaFechada && transform.eulerAngles.z <= 170)
        {
            playedSoundAlready = true;
            Debug.Log("Worked!");
            audio.clip = fechando;
            audio.Play();
        }
    }
    

    【讨论】:

    • 我试过你的解决方案,但现在它只播放“fechando”声音。 “abrindo”不再播放了。
    • @GuiVasconcelos 我在复制和粘贴内容时读错了角度。现在试试答案。
    • @GuiVasconcelos 我添加了一点来检查关闭声音是否已经在播放,因为您似乎还没有检查。
    • 谢谢您,但它仍然无法正常工作。我仍然只听到“fechando”而不是“abrindo”。如果我删除!从 portafechada 播放 abrindo 声音。我认为这是一个逻辑冲突。
    • @GuiVasconcelos 试试看。我正在检查 portaFechada 的错误值。我还更改了如何检查是否已播放关闭声音。让我知道这是否有效
    猜你喜欢
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多