質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

808閲覧

衝突音が出ません(複数音の出しわけ)

tsuppy

総合スコア4

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/08/17 13:55

物が落ちてくる衝突ゲームを作成しています。
りんごと爆弾の2種類をつくり、当たるとそれぞれ別の音が発生するように検討しています。
言語はunityです
お手数おかけしますが、ご回答いただけますと幸いです。

PlayerController

1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class PlayerController : MonoBehaviour 7{ 8 public AudioClip appleSE; 9 public AudioClip bombSE; 10 AudioSource aud; 11 GameObject director; 12 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 this.director = GameObject.Find("GameDirector"); 18 this.aud = GetComponent<AudioSource>(); 19 } 20 21 void OnTriggerEnter(Collider other) 22 { 23 if(other.gameObject.tag =="Apple") 24 { 25 this.director.GetComponent<GameDirector>().AddPoint(); 26 this.aud.PlayOneShot(this.appleSE); 27 } 28 else 29 { 30 this.director.GetComponent<GameDirector>().DecreaseHp(); 31 this.aud.PlayOneShot(this.bombSE); 32 } 33 } 34 35 // Update is called once per frame 36 void Update() 37 { 38 //左矢印が押された時 39 if(Input.GetKeyDown(KeyCode.LeftArrow)) 40 { 41 transform.Translate(-1, 0, 0); //左に「1」動かす 42 43 } 44 45 if(Input.GetKeyDown(KeyCode.RightArrow)) 46 { 47 transform.Translate(1, 0, 0); //右に「1」動かす 48 } 49 } 50} 51

BombController

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BombController : MonoBehaviour 6{ 7 GameObject gameFuwapon; 8 9 // Start is called before the first frame update 10 void Start() 11 { 12 this.gameFuwapon = GameObject.Find("gameFuwapon"); 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 //フレームごとに等速で落下させる 19 transform.Translate(0, -0.1f, 0); 20 21 //画面の外に出たらオブジェクトを破棄する 22 if(transform.position.y < -5.0f) 23 { 24 Destroy(gameObject); 25 } 26 27 //当たり判定 28 Vector2 p1 = transform.position; 29 Vector2 p2 = this.gameFuwapon.transform.position; 30 Vector2 dir = p1 - p2; 31 float d = dir.magnitude; 32 float r1 = 0.15f; //りんごの半径 33 float r2 = 1.0f; //プレイヤーの半径 34 35 if(d < r1 + r2) 36 { 37 //監督スクリプトにプレイヤーと衝突したことを伝える 38 GameObject director = GameObject.Find("GameDirector"); 39 director.GetComponent<GameDirector>().DecreaseHp(); 40 41 //衝突した場合は矢を消す 42 Destroy(gameObject); 43 } 44 } 45} 46

AppleController

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AppleController : MonoBehaviour 6{ 7 GameObject gameFuwapon; 8 public AudioClip appleSE; 9 AudioSource aud; 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 this.gameFuwapon = GameObject.Find("gameFuwapon"); 15 this.aud = GetComponent<AudioSource>(); 16 } 17 18 // Update is called once per frame 19 void Update() 20 { 21 //フレームごとに等速で落下させる 22 transform.Translate(0, -0.1f, 0); 23 24 //画面の外に出たらオブジェクトを破棄する 25 if(transform.position.y < -5.0f) 26 { 27 Destroy(gameObject); 28 } 29 30 //当たり判定 31 Vector2 p1 = transform.position; 32 Vector2 p2 = this.gameFuwapon.transform.position; 33 Vector2 dir = p1 - p2; 34 float d = dir.magnitude; 35 float r1 = 0.15f; //りんごの半径 36 float r2 = 1.0f; //プレイヤーの半径 37 38 if(d < r1 + r2) 39 { 40 //監督スクリプトにプレイヤーと衝突したことを伝える 41 GameObject director = GameObject.Find("GameDirector"); 42 this.aud.PlayOneShot(this.appleSE); 43 director.GetComponent<GameDirector>().AddPoint(); 44 45 //衝突した場合は矢を消す 46 Destroy(gameObject); 47 } 48 } 49} 50

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

  1. コードを見ると2Dっぽいですが、OnTriggerEnter は2D(Collider2D)では呼ばれません。OnTriggerEnter2D を使います。関数名にただ2Dと追加するだけではダメで、引数の型も正しく指定する必要があります。
  2. AudioSource コンポーネントが追加されている GameObject を、音が鳴らすのと同時に Destroy すると、音が聞こえる前に AudioSource も破棄されるので音は聞こえません。

投稿2021/08/17 14:32

bboydaisuke

総合スコア5275

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tsuppy

2021/08/18 14:07

ご回答ありがとうございます。初心者のため、手取り足取りな感じになってしまいすみません。OnTriggerEnter部分は、いま下記のようにコードを修正しました。 しかし、2つ目のアドバイス、Destroyの部分をどうやったら、音が聞こえる程度に指示出しできるのかがわかりません。なにか良い方法はありますか? void OnTriggerEnter2D(Collider2D other) { if(other.gameObject.tag =="Apple") { this.director.GetComponent<GameDirector>().AddPoint(); this.aud.PlayOneShot(this.appleSE); } else { this.director.GetComponent<GameDirector>().DecreaseHp(); this.aud.PlayOneShot(this.bombSE); } }
bboydaisuke

2021/08/18 14:43

とりあえず AudioSource.PlayClipAtPoint() で音を鳴らすことですね。一時しのぎであり、いずれ AudioSource の仕組みをもっとちゃんと知らなければいけないでしょうが、よくわからないならば PlayClipAtPoint でやればなんとかなります。 あと、インデントが左に寄ったコードを貼らないでください。
tsuppy

2021/08/19 13:10

ありがとうございます。コードの貼り付けがうまくできず、すみませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問