キーボードのボタンを押して複数の砲弾を切り替える方法
複数の砲弾を切り替える
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);
}
}
}
Unity Code Memo
他のコースを見る複数の砲弾を切り替える
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);
}
}
}
キーボードのボタンを押して複数の砲弾を切り替える方法