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?

More than 1 year has passed since last update.

【Unity】球・円柱の境界線から音を流すコンポーネント

Last updated at Posted at 2023-08-28

概要

gif.gif
球・円柱の境界線から音を流す実装で役立つコンポーネントを作りました。
具体的には、AudioListener から近い境界に AudioSource を移動させるコンポーネントです。

使用例

  • スプラトゥーン3のホップソナー
  • 巨大竜巻

留意点

円柱の回転には対応していません。常に境界の座標を返す Collider.ClosestPoint() のような関数があれば解決します。もしご存知の方がいましたらコメントをお願いします。

コード

使い方

便宜上、円柱の使い方を説明します。

  1. 円柱のオブジェクトを作成
  2. 円柱の子階層に AudioSource を作成
  3. AudioSource に適当なクリップを追加。loop を有効化。SpatialBlend を 1 に設定
  4. 円柱に BorderAudioSourceController を追加

各種設定

image.png

変数・プロパティ名 説明
centerTransform 球・円柱の中心
borderType 形状の指定
Sphere, Cylinder, CylinderInfiniteHeight があります
defaultRadius Scale が 1 の時の球・円柱の半径
defaultCylinderHeight Scale が 1 の時の円柱の高さ
cylinderPivotY centerTransform のY座標がCylinderのどの位置にあたるか
0 なら最下部、0.5 なら中心、1 なら最上部
childAudioSource 音を鳴らす AudioSource
autoUpdate audioSource の位置を自動で更新するフラグ
UpdateAudioSource() で手動更新もできます
listenerTransform audioSource の位置を計算する際に使う AudioListener の Transform
findListenerOnStart Start() 時、AudioListener を探すフラグ
外部から AudioListener をセットする事もできます

Spread の設定

Spread.png
左右の音量の急な切り替わりを防ぐため、Spread は距離が近いほど強くすると良いです。
ちなみに Spread を 0.5 以上にすると、見た目とは反対側から音が聴こえます。

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?