戦車から砲弾を発射する2(チャージ弾)









チャージ弾
using Unity.Mathematics;
using UnityEngine;
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;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
}
void Update()
{
if (isa.Player.Shot.triggered)
{
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;
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;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
}
void Update()
{
if (isa.Player.Shot.triggered)
{
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();
}
}


戦車から砲弾を発射する2(チャージ弾)