★Nonoka Iwabuchi

(2024年4月1日)

<ShotShellIconの表示の修正>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ShotShell : MonoBehaviour
{
    public float shotSpeed;
    public GameObject shellPrefab;
    public AudioClip shotSound;
    private float interval = 0.75f;
    private float timer = 0;
    public int shotCount;
    public GameObject[] ShellIcon;

    void Update()
    {        
        timer += Time.deltaTime;

        if (Input.GetKeyDown(KeyCode.Space) && timer > interval && shotCount > 0)
        {
            shotCount -= 1;

            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);
            AudioSource.PlayClipAtPoint(shotSound, transform.position);

            for (int i = 0; i < ShellIcon.Length; i++)
            {
                // ★コードの改良
                if (i < shotCount)
                {
                    ShellIcon[i].SetActive(true);
                }
                else
                {
                    ShellIcon[i].SetActive(false);
                }
            }

            // ★コメントアウト
            // if (shotCount == 10)
            // {
            //     SceneManager.LoadScene("GameOver");
            // }
        }
    }
}

(設定)