声音无法在 Unity 中播放
本文关键字:播放 Unity 声音 | 更新日期: 2023-09-27 18:35:22
大家好,我试图在你随着游戏移动时发出行走的声音。我尝试了很多方法和简单的audio.clip=walkSound和audio。玩,这些都不起作用。关于如何播放声音的任何想法?
public float walkSpeed = 5.0f;
public float slowPentaly = 0.5f;
public float gravity = 20.0f;
public float run = 2.0f;
private Vector3 moveDirection = Vector3.zero;
public AudioClip walkSound;
void Start()
{
//transform.position = new Vector3(0,1.7f,0);
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.tag == "Shard") {
moveDirection *= slowPentaly;
Debug.Log("You are slow");
}
}
void Update()
{
CharacterController controller = GetComponent<CharacterController> ();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= walkSpeed;
if (Input.GetKeyDown(KeyCode.LeftShift))
moveDirection *= run;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
}
音频剪辑是要播放的声音文件。这被放入播放它的音频源中。您不能"只"播放音频剪辑,您需要设置音频源。看看音频官方教程:http://unity3d.com/learn/tutorials/modules/beginner/audio/audio-sources-and-listeners
还要仔细检查摄像机(或场景中的某些对象)是否具有音频侦听器组件。