キーボードのボタンを押して複数の砲弾を切り替える方法
![Eb48b089 ce81 4993 a204 7a5eca8ae117](https://codegenius.org/uploads/slide/image/2320/eb48b089-ce81-4993-a204-7a5eca8ae117.jpeg)
複数の砲弾を切り替える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShotShell_Change : MonoBehaviour {
// (ポイント)
// 配列の定義
// 複数の砲弾データを入れる箱の作成
public GameObject[] shellPrefabs;
public float shotSpeed;
public GameObject shellIcon;
public AudioClip changeSound;
private int shellNumber = 0;
private Image image;
void Start(){
image = shellIcon.GetComponent<Image> ();
}
void Update () {
// 「各ボタン(z,x,c)」と「shellNumber」を紐づける。
if (Input.GetKeyDown (KeyCode.Z)) {
shellNumber = 0;
// アイコンの色を変える。
image.color = new Color (1, 0, 1, 1);
AudioSource.PlayClipAtPoint (changeSound, transform.position);
} else if (Input.GetKeyDown (KeyCode.X)) {
shellNumber = 1;
image.color = new Color (0, 0, 1, 1);
AudioSource.PlayClipAtPoint (changeSound, transform.position);
} else if (Input.GetKeyDown (KeyCode.C)) {
shellNumber = 2;
image.color = new Color (1, 0.92f, 0.016f, 1);
AudioSource.PlayClipAtPoint (changeSound, transform.position);
}
if (Input.GetKeyDown (KeyCode.Space)) {
// shellPrefabs[shellNumber]・・・>shellPrefabs[0]の砲弾、shellPrefabs[1]の砲弾、shellPrefabs[2]の砲弾
GameObject shell = (GameObject)Instantiate (shellPrefabs[shellNumber], transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody> ();
shellRb.AddForce (transform.forward * shotSpeed);
Destroy (shell, 3.0f);
}
}
}
![25eba31e dc60 4f94 be7f e0b4847816aa](https://codegenius.org/uploads/slide/image/2321/25eba31e-dc60-4f94-be7f-e0b4847816aa.jpeg)
![B9a75a5b f911 4663 8eb7 f173eb56e58c](https://codegenius.org/uploads/slide/image/2322/b9a75a5b-f911-4663-8eb7-f173eb56e58c.jpeg)
![Cd75ae54 9aca 4a46 9915 61b1df6e8ab1](https://codegenius.org/uploads/slide/image/2323/cd75ae54-9aca-4a46-9915-61b1df6e8ab1.jpeg)
![8936f6c6 6081 4fd1 8d9b a2acce1d9005](https://codegenius.org/uploads/slide/image/2324/8936f6c6-6081-4fd1-8d9b-a2acce1d9005.jpeg)
Unity Code Memo
他のコースを見る![Eb48b089 ce81 4993 a204 7a5eca8ae117](https://codegenius.org/uploads/slide/image/2320/eb48b089-ce81-4993-a204-7a5eca8ae117.jpeg)
複数の砲弾を切り替える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShotShell_Change : MonoBehaviour {
// (ポイント)
// 配列の定義
// 複数の砲弾データを入れる箱の作成
public GameObject[] shellPrefabs;
public float shotSpeed;
public GameObject shellIcon;
public AudioClip changeSound;
private int shellNumber = 0;
private Image image;
void Start(){
image = shellIcon.GetComponent<Image> ();
}
void Update () {
// 「各ボタン(z,x,c)」と「shellNumber」を紐づける。
if (Input.GetKeyDown (KeyCode.Z)) {
shellNumber = 0;
// アイコンの色を変える。
image.color = new Color (1, 0, 1, 1);
AudioSource.PlayClipAtPoint (changeSound, transform.position);
} else if (Input.GetKeyDown (KeyCode.X)) {
shellNumber = 1;
image.color = new Color (0, 0, 1, 1);
AudioSource.PlayClipAtPoint (changeSound, transform.position);
} else if (Input.GetKeyDown (KeyCode.C)) {
shellNumber = 2;
image.color = new Color (1, 0.92f, 0.016f, 1);
AudioSource.PlayClipAtPoint (changeSound, transform.position);
}
if (Input.GetKeyDown (KeyCode.Space)) {
// shellPrefabs[shellNumber]・・・>shellPrefabs[0]の砲弾、shellPrefabs[1]の砲弾、shellPrefabs[2]の砲弾
GameObject shell = (GameObject)Instantiate (shellPrefabs[shellNumber], transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody> ();
shellRb.AddForce (transform.forward * shotSpeed);
Destroy (shell, 3.0f);
}
}
}
![25eba31e dc60 4f94 be7f e0b4847816aa](https://codegenius.org/uploads/slide/image/2321/25eba31e-dc60-4f94-be7f-e0b4847816aa.jpeg)
![B9a75a5b f911 4663 8eb7 f173eb56e58c](https://codegenius.org/uploads/slide/image/2322/b9a75a5b-f911-4663-8eb7-f173eb56e58c.jpeg)
![Cd75ae54 9aca 4a46 9915 61b1df6e8ab1](https://codegenius.org/uploads/slide/image/2323/cd75ae54-9aca-4a46-9915-61b1df6e8ab1.jpeg)
![8936f6c6 6081 4fd1 8d9b a2acce1d9005](https://codegenius.org/uploads/slide/image/2324/8936f6c6-6081-4fd1-8d9b-a2acce1d9005.jpeg)
キーボードのボタンを押して複数の砲弾を切り替える方法