(改良)発射音の大きさを調整できるようにする
発射音の大きさの調整
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject shellPrefab;
public AudioClip shotSound;
private float interval = 0.75f;
private float timer = 0;
private int shotCount;
public TextMeshProUGUI shellLabel;
private int shotMaxCount = 20;
// ★追加(発射音)
private AudioSource audioS;
private void Start()
{
shotCount = shotMaxCount;
shellLabel.text = "" + shotCount;
// ★追加(発射音)
audioS = GetComponent<AudioSource>();
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > interval && shotCount > 0)
{
shotCount -= 1;
shellLabel.text = "" + shotCount;
timer = 0.0f;
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
// ★追加(発射音)
audioS.volume = 0.3f;
audioS.PlayOneShot(shotSound);
// 下記は使用しないのでコメントアウトする。
//AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
public void AddShell(int amount)
{
shotCount += amount;
if(shotCount > shotMaxCount)
{
shotCount = shotMaxCount;
}
shellLabel.text = "" + shotCount;
}
}
【2021版】BattleTank(基礎/全33回)
他のコースを見る発射音の大きさの調整
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject shellPrefab;
public AudioClip shotSound;
private float interval = 0.75f;
private float timer = 0;
private int shotCount;
public TextMeshProUGUI shellLabel;
private int shotMaxCount = 20;
// ★追加(発射音)
private AudioSource audioS;
private void Start()
{
shotCount = shotMaxCount;
shellLabel.text = "" + shotCount;
// ★追加(発射音)
audioS = GetComponent<AudioSource>();
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > interval && shotCount > 0)
{
shotCount -= 1;
shellLabel.text = "" + shotCount;
timer = 0.0f;
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
// ★追加(発射音)
audioS.volume = 0.3f;
audioS.PlayOneShot(shotSound);
// 下記は使用しないのでコメントアウトする。
//AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
public void AddShell(int amount)
{
shotCount += amount;
if(shotCount > shotMaxCount)
{
shotCount = shotMaxCount;
}
shellLabel.text = "" + shotCount;
}
}
(改良)発射音の大きさを調整できるようにする