(スクリプト)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ShotPower_Interval : MonoBehaviour
{
private int count;
private int maxPower = 100;
private int shotPower;
public GameObject shellPrefab;
public TextMeshProUGUI powerLabel;
private int maxInterval = 20;
private int interval;
public AudioClip shotSound;
private void Start()
{
shotPower = maxPower;
powerLabel.text = "POWER: " + shotPower;
interval = maxInterval;
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.Z) && shotPower > 0)
{
count += 1;
if (count % interval == 0)
{
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent();
shellRb.AddForce(transform.forward * 1000);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(shell, 5.0f);
shotPower -= 1;
if (shotPower < 1)
{
shotPower = 0;
// ★パワー回復
Invoke("ResetPower", 5.0f);
}
powerLabel.text = "POWER: " + shotPower;
// ★ポイント
if(shotPower % 25 == 0)
{
interval -= 5;
}
}
}
}
// 5秒後にショットパワー回復
void ResetPower()
{
shotPower = maxPower;
powerLabel.text = "POWER: " + shotPower;
// 発射間隔も初期状態に戻す。
interval = maxInterval;
}
}
(実行結果)