(Unity)リロードの基本ロジック

  • マウス左クリックで砲弾発射
  • 弾切れになったら、マウス右クリックでリロード

(スクリプト)

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

public class ShotShell_X : MonoBehaviour
{
    public GameObject shellPrefab;
    public AudioClip shotSound;
    public AudioClip reloadSound;
    public int shellCount;

    void Update()
    {
        if(Input.GetMouseButtonDown(0) && shellCount > 0)
        {
            shellCount -= 1;

            GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
            Rigidbody shellRb = shell.GetComponent<Rigidbody>();
            shellRb.AddForce(transform.forward * 500);
            AudioSource.PlayClipAtPoint(shotSound, Camera.main.transform.position);
            Destroy(shell, 5.0f);
        }

        if(Input.GetMouseButtonDown(1) && shellCount == 0)
        {
            shellCount = 3;
            AudioSource.PlayClipAtPoint(reloadSound, Camera.main.transform.position);
        }
    }
}