砲弾を発射する
![9cbc9219 739d 4fb2 ac82 1ee8740218c9](https://codegenius.org/uploads/slide/image/7218/9cbc9219-739d-4fb2-ac82-1ee8740218c9.jpeg)
![9d7e5d4d 6aa9 471b aa71 f38f9f19cde9](https://codegenius.org/uploads/slide/image/7219/9d7e5d4d-6aa9-471b-aa71-f38f9f19cde9.jpeg)
![55630e13 cdbc 4806 bc47 04019eb9d766](https://codegenius.org/uploads/slide/image/7220/55630e13-cdbc-4806-bc47-04019eb9d766.jpeg)
![A00b96db 8dc2 4d40 8f34 ebcc8981056a](https://codegenius.org/uploads/slide/image/7221/a00b96db-8dc2-4d40-8f34-ebcc8981056a.jpeg)
![Cb5170a3 9042 414c 9f9e 30b6116a211d](https://codegenius.org/uploads/slide/image/7222/cb5170a3-9042-414c-9f9e-30b6116a211d.jpeg)
![0987a9e8 a42c 4250 bc9c c2abdca7f8d4](https://codegenius.org/uploads/slide/image/7223/0987a9e8-a42c-4250-bc9c-c2abdca7f8d4.jpeg)
発射音を大きくする
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;
// ★追加
public GameObject audioX;
private void Start()
{
shotCount = shotMaxCount;
shellLabel.text = "" + shotCount;
}
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);
// ★改良
// audioX.transform.positionに変更
AudioSource.PlayClipAtPoint(shotSound, audioX.transform.position);
}
}
public void AddShell(int amount)
{
shotCount += amount;
if(shotCount > shotMaxCount)
{
shotCount = shotMaxCount;
}
shellLabel.text = "" + shotCount;
}
}
![5fb40b52 964b 4f6f 815d abc7d2c4d35b](https://codegenius.org/uploads/slide/image/7224/5fb40b52-964b-4f6f-815d-abc7d2c4d35b.jpeg)
![9cbc9219 739d 4fb2 ac82 1ee8740218c9](https://codegenius.org/uploads/slide/image/7218/9cbc9219-739d-4fb2-ac82-1ee8740218c9.jpeg)
![9d7e5d4d 6aa9 471b aa71 f38f9f19cde9](https://codegenius.org/uploads/slide/image/7219/9d7e5d4d-6aa9-471b-aa71-f38f9f19cde9.jpeg)
![55630e13 cdbc 4806 bc47 04019eb9d766](https://codegenius.org/uploads/slide/image/7220/55630e13-cdbc-4806-bc47-04019eb9d766.jpeg)
![A00b96db 8dc2 4d40 8f34 ebcc8981056a](https://codegenius.org/uploads/slide/image/7221/a00b96db-8dc2-4d40-8f34-ebcc8981056a.jpeg)
![Cb5170a3 9042 414c 9f9e 30b6116a211d](https://codegenius.org/uploads/slide/image/7222/cb5170a3-9042-414c-9f9e-30b6116a211d.jpeg)
![0987a9e8 a42c 4250 bc9c c2abdca7f8d4](https://codegenius.org/uploads/slide/image/7223/0987a9e8-a42c-4250-bc9c-c2abdca7f8d4.jpeg)
発射音を大きくする
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;
// ★追加
public GameObject audioX;
private void Start()
{
shotCount = shotMaxCount;
shellLabel.text = "" + shotCount;
}
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);
// ★改良
// audioX.transform.positionに変更
AudioSource.PlayClipAtPoint(shotSound, audioX.transform.position);
}
}
public void AddShell(int amount)
{
shotCount += amount;
if(shotCount > shotMaxCount)
{
shotCount = shotMaxCount;
}
shellLabel.text = "" + shotCount;
}
}
![5fb40b52 964b 4f6f 815d abc7d2c4d35b](https://codegenius.org/uploads/slide/image/7224/5fb40b52-964b-4f6f-815d-abc7d2c4d35b.jpeg)
砲弾を発射する