猿问

有人可以帮我让我的音频在 Unity 中播放吗?

我对 C# 有点陌生,几天前我才开始使用 Unity。我正在尝试设置一个脚本,当赛车行驶时,您会听到发动机的声音,但是如果赛车停下来,您就不会听到发动机的声音。但是,当汽车停止移动时,我一直无法让音频停止播放。这是程序:


using UnityEngine;



public class RaceCarMovement : MonoBehaviour {

    // Use this for initialization    


    float drivespeed = 0.3f;

    private AudioSource CarEngine;


    private void Awake()

    {

        CarEngine = GetComponent<AudioSource>();

    }


    void Start () {

    }


    // Update is called once per frame

    void Update()

    {


        if (Input.GetKey("up") == true || Input.GetKey("down") == true || Input.GetKey("left") == true || Input.GetKey("right") == true)

        {

            Drive();

            CarEngine.Play();

        }


        else

        {

            if (CarEngine.isPlaying)

            {

                CarEngine.Stop();

            }

        }



    }

    public void Drive()

    {

        if (Input.GetKey("up") == true)

        {

            transform.position = new Vector3(transform.position.x + drivespeed, transform.position.y);

        }


        if (Input.GetKey("down") == true)

        {

            transform.position = new Vector3(transform.position.x - drivespeed, transform.position.y);

        }


        if (Input.GetKey("left") == true)

        {

            transform.position = new Vector3(transform.position.x, transform.position.y + drivespeed);

        }


        if (Input.GetKey("right") == true)

        {

            transform.position = new Vector3(transform.position.x, transform.position.y - drivespeed);

        }

    } 

}

我不明白 else 语句如何不起作用,因为 Update() 应该更新每一帧。有没有人有任何建议/解释?


拉风的咖菲猫
浏览 199回答 2
2回答

噜噜哒

你只需要在播放音乐之前添加一个条件来检查它是否已经在播放。喜欢....void Update(){&nbsp; &nbsp; if (Input.GetKey("up") == true || Input.GetKey("down") == true || Input.GetKey("left") == true || Input.GetKey("right") == true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Drive();&nbsp; &nbsp; &nbsp; &nbsp; if (!CarEngine.isPlaying)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CarEngine.Play();&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (CarEngine.isPlaying)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Stop playing....");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CarEngine.Stop();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

跃然一笑

首先,我看到有一些不必要的代码行,我们将消除它们。然后,我们创建了一个方法来使用一个布尔参数来重现声音,该参数将用于重现或停止音频,这个参数将是一个布尔值,那么只有当该布尔值为真时才会拒绝音频,所以你必须说它按下一个键时为真。否则,如果您简化工作,实际上您正在验证从第一个上面到最后一个 else 中的哪一个满足,如果满足则执行第一行,否则执行 else。using UnityEngine;public class RaceCarMovement : MonoBehaviour {&nbsp; &nbsp; float drivespeed = 0.3f;&nbsp; &nbsp; private AudioSource CarEngine;&nbsp; &nbsp; private void Awake()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; CarEngine = GetComponent<AudioSource>();&nbsp; &nbsp; }&nbsp; &nbsp; void Start ()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; void Update()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Input.GetKey("up"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.position = new Vector3(transform.position.x + drivespeed, transform.position.y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlayCarSound(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (Input.GetKey("down"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.position = new Vector3(transform.position.x - drivespeed, transform.position.y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlayCarSound(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (Input.GetKey("left"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.position = new Vector3(transform.position.x, transform.position.y + drivespeed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlayCarSound(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (Input.GetKey("right"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.position = new Vector3(transform.position.x, transform.position.y - drivespeed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlayCarSound(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PlayCarSound(false);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void PlayCarSound(bool play)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(play /*&& !CarEngine.isPlaying*/) CarEngine.Play();&nbsp; &nbsp; &nbsp; &nbsp; else CarEngine.Stop();&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答