(改良)効果音の大きさを調整できるようにする


効果音の音量調整
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
private int coinCount = 0;
private Vector3 pos;
public GameObject[] coinIcons;
public GameObject key;
// ★追加(効果音)
private AudioSource audioS;
void Start()
{
rb = GetComponent<Rigidbody>();
// ★追加(効果音)
audioS = GetComponent<AudioSource>();
}
void Update()
{
pos = transform.position;
if(pos.y < -10)
{
SceneManager.LoadScene("GameOver");
}
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
// ★追加(効果音)
audioS.volume = 0.5f; // 音の大きさ(volume)の調整
audioS.PlayOneShot(coinGet);
// ★変更(効果音)
// 下記のコードは「//」を付けてコメントアウトにする。
//AudioSource.PlayClipAtPoint(coinGet, transform.position);
coinCount += 1;
for (int i = 0; i < coinIcons.Length; i++)
{
if(coinCount <= i)
{
coinIcons[i].SetActive(true);
}
else
{
coinIcons[i].SetActive(false);
}
}
if(coinCount == 1)
{
key.SetActive(true);
}
if (coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}


【2021版】BallGame(全31回)
他のコースを見る

効果音の音量調整
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
private int coinCount = 0;
private Vector3 pos;
public GameObject[] coinIcons;
public GameObject key;
// ★追加(効果音)
private AudioSource audioS;
void Start()
{
rb = GetComponent<Rigidbody>();
// ★追加(効果音)
audioS = GetComponent<AudioSource>();
}
void Update()
{
pos = transform.position;
if(pos.y < -10)
{
SceneManager.LoadScene("GameOver");
}
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
// ★追加(効果音)
audioS.volume = 0.5f; // 音の大きさ(volume)の調整
audioS.PlayOneShot(coinGet);
// ★変更(効果音)
// 下記のコードは「//」を付けてコメントアウトにする。
//AudioSource.PlayClipAtPoint(coinGet, transform.position);
coinCount += 1;
for (int i = 0; i < coinIcons.Length; i++)
{
if(coinCount <= i)
{
coinIcons[i].SetActive(true);
}
else
{
coinIcons[i].SetActive(false);
}
}
if(coinCount == 1)
{
key.SetActive(true);
}
if (coinCount == 3)
{
SceneManager.LoadScene("GameClear");
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}


(改良)効果音の大きさを調整できるようにする