c# - 在 Unity5 中编写音频脚本

标签 c# audio unity3d

大家好! 我在 Unity 5 和 C# 中启动,我正在学习一个教程,事情是......该教程是为 Unity 4 制作的,因此教程中的一些代码在 U5 中不可用。这次我的问题是将音频编写成一个 Action ,代码如下:

using UnityEngine;
using System.Collections;

[System.Serializable]

public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{

public float speed;
public float tilt;
public Boundary boundary;

public GameObject shot;
public float fireRate;
public Transform shotSpawn;

private float nextFire;

void Update()
{
    if (Input.GetButton ("Fire1") && Time.time > nextFire)
    {
        nextFire = Time.time + fireRate;
        Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
        Audio.Play(); <---
    }
}

void FixedUpdate ()
{
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    Rigidbody rb = GetComponent<Rigidbody> ();
    rb.velocity = movement * speed;

    rb.position = new Vector3 
        (
            Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
        );
    rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
}

好了,行“audio.Play();”未编译,因为 U5 中没有任何东西甚至接近该语法。有人可以在这里给我提示吗?

提前致谢!

最佳答案

而不是使用 Audio.play()使用 GetComponent<AudioSource>().Play();并确保您已将音频源附加到游戏对象。

关于c# - 在 Unity5 中编写音频脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29143816/

相关文章:

c# - 谷歌 Firebase 和 Unity (C#) : Unable to download png from bucket

c# - unity ParticleSystem 播放和停止

c# - 关于在 Linq 中重用 db context 的意见

c# - 无法在 Sitefinity MVC Controller 操作中使用异步/等待

Swift - 如何从麦克风输入(AVAudioPCMBuffer)获取当前音量

java - 在Android中播放指定时间的音频

c# - 为什么依赖于平台的编译不起作用?

c# - 域驱动应用程序中的集合过滤逻辑应该放在哪里?

c# - 此 C 函数的正确 C# PInvoke 签名

audio - 使用多个音频流ffmpeg时不能使用-shortest参数