1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】AudioSource の PlayOneShot で同じ音を多重再生する際の違和感を軽減する手法

Posted at

概要

AudioSource.PlayOneShot() を使えば多重再生できますが、同じ音を何度も再生すると単調で違和感が生まれます。
音にバリエーションを用意しランダム再生すれば改善しますが、メモリを食います。

再生時にピッチをランダムな値に変更する手法ならより少ないバリエーションで違和感を軽減できます。しかし再生中の音のピッチが急に変わるので、別の違和感が発生する恐れもあります。

手法紹介

そこで私は AudioSource のピッチを PerlinNoise でランダムかつ滑らかに変動させ続ける手法を考えました。
1つの音に限らず、複数種類の音を多重再生しても自然に聞こえると思います。

    public void UpdatePitch()
    {
        audioSource.pitch = Pitch - PitchRange / 2 + PitchRange * Mathf.PerlinNoise1D(Time.time * NoiseFrequency);
    }

実装例

AudioSourcePitchNoisier.cs
using UnityEngine;

[RequireComponent (typeof(AudioSource))]
public class AudioSourcePitchNoisier : MonoBehaviour
{
    [SerializeField] public float Pitch = 1f;
    [SerializeField] public float PitchRange = 0.1f;
    [SerializeField] public float NoiseFrequency = 1f;

    private AudioSource audioSource;

    private void Awake()
    {
        audioSource = GetComponent<AudioSource>();
    }

    private void Update()
    {
        UpdatePitch();
    }

    public void UpdatePitch()
    {
        audioSource.pitch = Pitch - PitchRange / 2 + PitchRange * Mathf.PerlinNoise1D(Time.time * NoiseFrequency);
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?