大家好! 我在 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/