ここから銃弾が出るたびに音が鳴るようににしたいです。
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
こちらの質問が複数のユーザーから「調査したこと・試したことが記載されていない質問」という指摘を受けました。
回答2件
あなたの回答
tips
プレビュー