มาเริ่มกันเลย
1. ให้ทำการสร้าง Scene และ Character + Animation ให้พร้อม
2. จากนั้นทำการสร้าง Animator สำหรับตัวละครและเชื่อม Transaction ดังรูป
using UnityEngine;
public class JumpSoundManager : StateMachineBehaviour
{
public AudioClip[] jumpClip;
private AudioSource audioSource;
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
audioSource = Camera.main.GetComponent<AudioSource>();
int index = Random.Range(0, 2);
Debug.Log(index);
audioSource.clip = jumpClip[index];
audioSource.Play();
}
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
audioSource.Stop();
}
}
โดยสคริปจะเริ่มทำงานจาก Function OnStateEnter (Random Play Sound Clip) หลังจากที่ Animation Transaction กลับไปยัง Idle จะทำการ Run Function OnStateExit (Stop Sound)
6. จากนั้นให้ทำการ Add AudioSource Component ให้กับ Main Camera
เท่านี้ก็สามารถนำไปประยุกต์ใช้ได้แล้วครับ
มีอะไรแนะนำได้นะครับ
NO GAME NO DREAM!!!