戦車から砲弾を発射する2(放物線軌道)








砲弾の発射(放物線軌道)
using UnityEngine; public class ShotShell : MonoBehaviour { private InputSystem_Actions isa; public GameObject shellPrefab; 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 += 15f; // パワーの上限設定 if (shotPower > 900f) { shotPower = 900f; } } // ★追加 // ボタンから指を離した時(条件) if (isa.Player.Shot2.WasReleasedThisFrame()) { 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; } } private void OnDisable() { isa.Disable(); } }
C#


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







砲弾の発射(放物線軌道)
using UnityEngine; public class ShotShell : MonoBehaviour { private InputSystem_Actions isa; public GameObject shellPrefab; 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 += 15f; // パワーの上限設定 if (shotPower > 900f) { shotPower = 900f; } } // ★追加 // ボタンから指を離した時(条件) if (isa.Player.Shot2.WasReleasedThisFrame()) { 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; } } private void OnDisable() { isa.Disable(); } }
C#


戦車から砲弾を発射する2(放物線軌道)