Ways to Unity 11: How to add music(audio) to your game
Introduction
You’ll need two components to be able to work.
The first one is the Audio Listener
, the second one is the Audio Source
.
If you want to play the background music, drag and drop your song to the AudioClip
slot. Then enable Play On Awake
and Loop
options.
If you want to play some audio on some events, you’ll have to do the programming yourself.
Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioClip jumpSound;
private AudioSource playerAudio;
void Start()
{
playerAudio = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
playerAudio.PlayOneShot(jumpSound, 1.0f);
}
}
}
The author
https://yingshaoxo.blogspot.com/2020/12/ways-to-unity-11-how-to-add-musicaudio.html