残弾数を表示する/弾切れの実装





弾切れの実装
using Unity.Mathematics;
using UnityEngine;
// ★追加
using TMPro;
public class ShotShell : MonoBehaviour
{
private InputSystem_Actions isa;
public GameObject shellPrefab;
public GameObject shellPrefab2;
public AudioClip shotSound;
public float shotSpeed;
public GameObject shotPoint;
private float shotPower;
// ★追加
public int shellCount;
public TextMeshProUGUI shellLabel;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
// ★追加
shellLabel.text = "" + shellCount;
}
void Update()
{
if (isa.Player.Shot.triggered)
{
// ★追加
if (shellCount < 1)
{
return;
}
// ★追加
shellCount -= 1;
shellLabel.text = "" + shellCount;
GameObject shell = Instantiate(shellPrefab, shotPoint.transform.position, quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
if (isa.Player.Shot2.IsPressed())
{
shotPower += 2;
if (shotPower > 1000)
{
shotPower = 1000;
}
}
if (isa.Player.Shot2.WasReleasedThisFrame())
{
GameObject shell = Instantiate(shellPrefab2, shotPoint.transform.position, quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotPower);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(shell, 5.0f);
shotPower = 0;
}
}
void OnDisable()
{
isa.Disable();
}
}



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




弾切れの実装
using Unity.Mathematics;
using UnityEngine;
// ★追加
using TMPro;
public class ShotShell : MonoBehaviour
{
private InputSystem_Actions isa;
public GameObject shellPrefab;
public GameObject shellPrefab2;
public AudioClip shotSound;
public float shotSpeed;
public GameObject shotPoint;
private float shotPower;
// ★追加
public int shellCount;
public TextMeshProUGUI shellLabel;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
// ★追加
shellLabel.text = "" + shellCount;
}
void Update()
{
if (isa.Player.Shot.triggered)
{
// ★追加
if (shellCount < 1)
{
return;
}
// ★追加
shellCount -= 1;
shellLabel.text = "" + shellCount;
GameObject shell = Instantiate(shellPrefab, shotPoint.transform.position, quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
if (isa.Player.Shot2.IsPressed())
{
shotPower += 2;
if (shotPower > 1000)
{
shotPower = 1000;
}
}
if (isa.Player.Shot2.WasReleasedThisFrame())
{
GameObject shell = Instantiate(shellPrefab2, shotPoint.transform.position, quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotPower);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(shell, 5.0f);
shotPower = 0;
}
}
void OnDisable()
{
isa.Disable();
}
}



残弾数を表示する/弾切れの実装