戦車から砲弾を発射する
![E2967535 2f91 4979 83e5 b5c5f21eef83](https://codegenius.org/uploads/slide/image/1401/e2967535-2f91-4979-83e5-b5c5f21eef83.jpeg)
![C44a20a8 cb47 43d7 aa1f ac18909314f9](https://codegenius.org/uploads/slide/image/1402/c44a20a8-cb47-43d7-aa1f-ac18909314f9.jpeg)
![C47c390b 922e 4ccd 918c c4c9d30bc9b3](https://codegenius.org/uploads/slide/image/1403/c47c390b-922e-4ccd-918c-c4c9d30bc9b3.jpeg)
![0c3ce003 59ac 4d51 8054 3cbb871dfa34](https://codegenius.org/uploads/slide/image/1404/0c3ce003-59ac-4d51-8054-3cbb871dfa34.jpeg)
砲弾を発射する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
void Update()
{
// もしもSpaceキーを押したならば(条件)
// 「Space」の部分を変更することで他のキーにすることができる(ポイント)
if (Input.GetKeyDown(KeyCode.Space))
{
// 砲弾のプレハブを実体化(インスタンス化)する。
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
// 砲弾に付いているRigidbodyコンポーネントにアクセスする。
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
// forward(青軸=Z軸)の方向に力を加える。
shellRb.AddForce(transform.forward * shotSpeed);
// 発射した砲弾を3秒後に破壊する。
// (重要な考え方)不要になった砲弾はメモリー上から削除すること。
Destroy(shell, 3.0f);
// 砲弾の発射音を出す。
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
}
![66241ef5 c442 41d7 aa55 5f2b20c50210](https://codegenius.org/uploads/slide/image/1405/66241ef5-c442-41d7-aa55-5f2b20c50210.jpeg)
![Cd861ae8 b473 4202 a02e d66998d922d9](https://codegenius.org/uploads/slide/image/1406/cd861ae8-b473-4202-a02e-d66998d922d9.jpeg)
![E2f3ddf7 5d16 41bc a538 6b90be7d4041](https://codegenius.org/uploads/slide/image/1407/e2f3ddf7-5d16-41bc-a538-6b90be7d4041.jpeg)
![53c2560c 604a 4ebf 88c5 7d38ea29208e](https://codegenius.org/uploads/slide/image/1408/53c2560c-604a-4ebf-88c5-7d38ea29208e.jpeg)
![F87a1ec9 719b 42d3 b2b2 f86821c016d7](https://codegenius.org/uploads/slide/image/1409/f87a1ec9-719b-42d3-b2b2-f86821c016d7.jpeg)
![D057b7d9 957c 468e a7b1 4dc4daa78d62](https://codegenius.org/uploads/slide/image/1410/d057b7d9-957c-468e-a7b1-4dc4daa78d62.jpeg)
![8cce759c f303 4e36 930c dcf60868f39e](https://codegenius.org/uploads/slide/image/1411/8cce759c-f303-4e36-930c-dcf60868f39e.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![E2967535 2f91 4979 83e5 b5c5f21eef83](https://codegenius.org/uploads/slide/image/1401/e2967535-2f91-4979-83e5-b5c5f21eef83.jpeg)
![C44a20a8 cb47 43d7 aa1f ac18909314f9](https://codegenius.org/uploads/slide/image/1402/c44a20a8-cb47-43d7-aa1f-ac18909314f9.jpeg)
![C47c390b 922e 4ccd 918c c4c9d30bc9b3](https://codegenius.org/uploads/slide/image/1403/c47c390b-922e-4ccd-918c-c4c9d30bc9b3.jpeg)
![0c3ce003 59ac 4d51 8054 3cbb871dfa34](https://codegenius.org/uploads/slide/image/1404/0c3ce003-59ac-4d51-8054-3cbb871dfa34.jpeg)
砲弾を発射する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
void Update()
{
// もしもSpaceキーを押したならば(条件)
// 「Space」の部分を変更することで他のキーにすることができる(ポイント)
if (Input.GetKeyDown(KeyCode.Space))
{
// 砲弾のプレハブを実体化(インスタンス化)する。
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
// 砲弾に付いているRigidbodyコンポーネントにアクセスする。
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
// forward(青軸=Z軸)の方向に力を加える。
shellRb.AddForce(transform.forward * shotSpeed);
// 発射した砲弾を3秒後に破壊する。
// (重要な考え方)不要になった砲弾はメモリー上から削除すること。
Destroy(shell, 3.0f);
// 砲弾の発射音を出す。
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
}
![66241ef5 c442 41d7 aa55 5f2b20c50210](https://codegenius.org/uploads/slide/image/1405/66241ef5-c442-41d7-aa55-5f2b20c50210.jpeg)
![Cd861ae8 b473 4202 a02e d66998d922d9](https://codegenius.org/uploads/slide/image/1406/cd861ae8-b473-4202-a02e-d66998d922d9.jpeg)
![E2f3ddf7 5d16 41bc a538 6b90be7d4041](https://codegenius.org/uploads/slide/image/1407/e2f3ddf7-5d16-41bc-a538-6b90be7d4041.jpeg)
![53c2560c 604a 4ebf 88c5 7d38ea29208e](https://codegenius.org/uploads/slide/image/1408/53c2560c-604a-4ebf-88c5-7d38ea29208e.jpeg)
![F87a1ec9 719b 42d3 b2b2 f86821c016d7](https://codegenius.org/uploads/slide/image/1409/f87a1ec9-719b-42d3-b2b2-f86821c016d7.jpeg)
![D057b7d9 957c 468e a7b1 4dc4daa78d62](https://codegenius.org/uploads/slide/image/1410/d057b7d9-957c-468e-a7b1-4dc4daa78d62.jpeg)
![8cce759c f303 4e36 930c dcf60868f39e](https://codegenius.org/uploads/slide/image/1411/8cce759c-f303-4e36-930c-dcf60868f39e.jpeg)
戦車から砲弾を発射する