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





残弾数の表示
using UnityEngine;
// ★追加
using TMPro;
public class ShotShell : MonoBehaviour
{
private InputSystem_Actions isa;
public GameObject shellPrefab;
public AudioClip shotSound;
public float shotSpeed;
private float shotPower;
public GameObject shotPoint;
// ★追加
public int shellCount;
public TextMeshProUGUI shellLabel;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
// ★追加
shellLabel.text = "" + shellCount;
}
void Update()
{
// ★追加
if (shellCount < 1)
{
// 弾切れの実装(テクニック)
return;
}
if (isa.Player.Shot.triggered)
{
// ★追加
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 += 15f;
if (shotPower > 900f)
{
shotPower = 900f;
}
}
if (isa.Player.Shot2.WasReleasedThisFrame())
{
// ★追加
shellCount -= 1;
shellLabel.text = "" + shellCount;
GameObject shell = Instantiate(shellPrefab, shotPoint.transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.useGravity = true;
shellRb.AddForce((transform.forward + new Vector3(0, 0.5f, 0)) * shotPower);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(shell, 5.0f);
shotPower = 0;
}
}
}


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




残弾数の表示
using UnityEngine;
// ★追加
using TMPro;
public class ShotShell : MonoBehaviour
{
private InputSystem_Actions isa;
public GameObject shellPrefab;
public AudioClip shotSound;
public float shotSpeed;
private float shotPower;
public GameObject shotPoint;
// ★追加
public int shellCount;
public TextMeshProUGUI shellLabel;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
// ★追加
shellLabel.text = "" + shellCount;
}
void Update()
{
// ★追加
if (shellCount < 1)
{
// 弾切れの実装(テクニック)
return;
}
if (isa.Player.Shot.triggered)
{
// ★追加
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 += 15f;
if (shotPower > 900f)
{
shotPower = 900f;
}
}
if (isa.Player.Shot2.WasReleasedThisFrame())
{
// ★追加
shellCount -= 1;
shellLabel.text = "" + shellCount;
GameObject shell = Instantiate(shellPrefab, shotPoint.transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.useGravity = true;
shellRb.AddForce((transform.forward + new Vector3(0, 0.5f, 0)) * shotPower);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
Destroy(shell, 5.0f);
shotPower = 0;
}
}
}


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