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

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

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

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

2回答

963閲覧

銃撃音の出し方が分かりません

Leafa

総合スコア16

C#

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

0クリップ

投稿2022/08/29 16:33

ここから銃弾が出るたびに音が鳴るようににしたいです。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Weapon : MonoBehaviour 6{ 7 8 public Transform shotDirection; 9 10 public AudioSource weapon; 11 public AudioClip relodingSE, fireSE, triggerSE; 12 13 public static Weapon instance; 14 // Start is called before the first frame update 15 void Start() 16 { 17 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 Debug.DrawRay(shotDirection.position, shotDirection.transform.forward * 10, Color.blue); 24 } 25 26 public void CanShoot() 27 { 28 GameState.canShoot = true; 29 } 30 31 public void Shooting() 32 { 33 RaycastHit hitInfo; 34 35 if (Physics.Raycast(shotDirection.transform.position, shotDirection.transform.forward, out hitInfo, 300)) 36 { 37 if(hitInfo.collider.gameObject.GetComponent<ZombieController>() != null) 38 { 39 ZombieController hitZombie = hitInfo.collider.gameObject.GetComponent<ZombieController>(); 40 41 hitZombie.ZombieDeath(); 42 } 43 } 44 } 45 46 public void FireSE() 47 { 48 weapon.clip = fireSE; 49 weapon.Play(); 50 } 51 52 public void RelodingSE() 53 { 54 weapon.clip = relodingSE; 55 weapon.Play(); 56 } 57 58 public void TriggerSE() 59 { 60 weapon.clip = triggerSE; 61 weapon.Play(); 62 } 63} 64

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class FPSController : MonoBehaviour 7{ 8 9 float x, z; 10 public float speed = 0.1f; 11 12 public GameObject cam; 13 Quaternion cameraRot, characterRot; 14 15 float Xsensityvity = 3f, Ysensityvity = 3f; 16 17 bool cursorLock = true; 18 19 float minX = -90f, maxX = 90f; 20 21 public Animator animator; 22 23 public int ammunition = 50, maxAmmunition = 50, ammoClip = 10, maxAmmoClip = 10; 24 25 public int playerHp = 100, maxPlayerHP = 100; 26 public Slider hpBer; 27 public Text ammoText; 28 29 public GameObject mainCamera, subCamera; 30 31 // Start is called before the first frame update 32 void Start() 33 { 34 cameraRot = cam.transform.localRotation; 35 characterRot = transform.localRotation; 36 37 GameState.canShoot = true; 38 39 hpBer.value = playerHp; 40 ammoText.text = ammoClip + "/" + ammunition; 41 42 } 43 44 // Update is called once per frame 45 void Update() 46 { 47 float xRot = Input.GetAxis("Mouse X") * Ysensityvity; 48 float yRot = Input.GetAxis("Mouse Y") * Xsensityvity; 49 50 cameraRot *= Quaternion.Euler(-yRot, 0, 0); 51 characterRot *= Quaternion.Euler(0, xRot, 0); 52 53 54 cameraRot = ClampRotation(cameraRot); 55 56 cam.transform.localRotation = cameraRot; 57 transform.localRotation = characterRot; 58 59 UpdateCursorLook(); 60 61 if (Input.GetMouseButton(0) && GameState.canShoot) 62 { 63 if (ammoClip > 0) 64 { 65 animator.SetTrigger("Fire"); 66 GameState.canShoot = false; 67 68 ammoClip--; 69 ammoText.text = ammoClip + "/" + ammunition; 70 71 } 72 else 73 { 74 // Debug.Log("弾薬 0"); 75 } 76 77 78 } 79 80 if (Input.GetKeyDown(KeyCode.R)) 81 { 82 int amountNeed = maxAmmoClip - ammoClip; 83 int ammoAvailable = amountNeed < ammunition ? amountNeed : ammunition; 84 85 if(amountNeed != 0 && ammunition != 0) 86 { 87 animator.SetTrigger("Reload"); 88 89 ammunition -= ammoAvailable; 90 ammoClip += ammoAvailable; 91 ammoText.text = ammoClip + "/" + ammunition; 92 } 93 94 95 } 96 97 if (Mathf.Abs(x) > 0|| Mathf.Abs(z) > 0) 98 { 99 if (!animator.GetBool("Walk")) 100 { 101 animator.SetBool("Walk",true); 102 } 103 } 104 else if (animator.GetBool("Walk")) 105 { 106 animator.SetBool("Walk", false); 107 } 108 109 if (z > 0 && Input.GetKey(KeyCode.LeftShift)) 110 { 111 if (!animator.GetBool("Run")) 112 { 113 animator.SetBool("Run", true); 114 speed = 0.25f; 115 } 116 } 117 else if (animator.GetBool("Run")) 118 { 119 animator.SetBool("Run", false); 120 speed = 0.1f; 121 } 122 123 if (Input.GetMouseButton(1)) 124 { 125 subCamera .SetActive(true); 126 mainCamera.GetComponent<Camera>().enabled = false; 127 } 128 129 else if (subCamera.activeSelf) 130 { 131 subCamera.SetActive(false); 132 mainCamera.GetComponent<Camera>().enabled = true; 133 } 134 } 135 136 private void FixedUpdate() 137 { 138 x = 0; 139 z = 0; 140 141 x = Input.GetAxisRaw("Horizontal") * speed; 142 z = Input.GetAxisRaw("Vertical") * speed; 143 144 //transform.position += new Vector3(x, 0, z); 145 transform.position +=cam.transform.forward * z + cam.transform.right * x; 146 } 147 148 public void UpdateCursorLook() 149 { 150 if(Input.GetKeyDown(KeyCode.Escape)) 151 { 152 cursorLock = false; 153 } 154 else if (Input.GetMouseButton(0)) 155 { 156 cursorLock= true; 157 } 158 159 if (cursorLock) 160 { 161 Cursor.lockState = CursorLockMode.Locked; 162 } 163 164 else if (!cursorLock) 165 { 166 Cursor.lockState= CursorLockMode.None; 167 } 168 } 169 170 public Quaternion ClampRotation(Quaternion q) 171 { 172 q.x /= q.w; 173 q.y /= q.w; 174 q.z /= q.w; 175 q.w = 1f; 176 177 float angleX = Mathf.Atan(q.x) * Mathf.Rad2Deg * 2f; 178 179 angleX = Mathf.Clamp(angleX, minX, maxX); 180 181 q.x = Mathf.Tan(angleX * Mathf.Deg2Rad * 0.5f); 182 183 return q; 184 } 185 186 public void TakeHit(float damage) 187 { 188 playerHp =(int) Mathf.Clamp(playerHp - damage, 0, playerHp); 189 190 hpBer.value = playerHp; 191 192 if(playerHp <= 0 && !GameState.GameOver) 193 { 194 GameState.GameOver = true; 195 } 196 } 197} 198 199

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

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

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

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

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

fiveHundred

2022/08/30 01:48

現状では丸投げとみなされても仕方がない内容です。 音声の出し方は基本的な内容なので、調べればすぐに分かるはずです。 もし、それでも上手くいかないのであれば、(どのようなコードで)何を試したのか、何を参考にしたのかを記載してください。
Leafa

2022/08/30 03:38

音声素材を入れるためのコードは分かったのですが銃弾が出るごとにその音を出す方法が分からなかったため質問させていただきました。
guest

回答2

0

自己解決

ありがとうございます!やってみます!

投稿2022/08/30 04:08

Leafa

総合スコア16

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

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

0

C#

1 public void FireSE() 2 { 3 weapon.clip = fireSE; 4 weapon.Play(); 5 } 6 7 public void RelodingSE() 8 { 9 weapon.clip = relodingSE; 10 weapon.Play(); 11 } 12 13 public void TriggerSE() 14 { 15 weapon.clip = triggerSE; 16 weapon.Play(); 17 }

とあるのだから、これらの関数を再生したいタイミングで呼び出せばいいのでは?

投稿2022/08/30 03:46

fiveHundred

総合スコア9805

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問