実現したいこと
Unityで音楽ゲームの製作をしています。
生成して落ちてきたオブジェクトに対して特定のキーを押すとそのオブジェクトを消えるというプログラムにしたいです。
発生している問題
生成して落ちてきたオブジェクトに対して特定のキーを押してもオブジェクトが消えません.
ちなみにUnityは実行できています。
該当のソースコード
ここでキーの割り当てをしています。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public static class GameUtil { 6 7 public static KeyCode GetKeyCodeByLineNum (int lineNum) { 8 switch (lineNum) { 9 case 0: 10 return KeyCode.F; 11 case 1: 12 return KeyCode.G; 13 case 2: 14 return KeyCode.H; 15 case 3: 16 return KeyCode.J; 17 default: 18 return KeyCode.None; 19 } 20 } 21} 22
ここでは生成されたオブジェクトを落ちるようにするプログラムと特定のキーを押したときにDestroy関数を作動するようにしています。
ここのCheckInput関数が反応していません。
C#
1using System.Collections; 2using UnityEngine; 3 4public class NoteScript : MonoBehaviour { 5 6 public int lineNum; 7 private GameManager _gameManager; 8 private bool isInLine = false; 9 private KeyCode _lineKey; 10 11 void Start () { 12 _gameManager = GameObject.Find ("GameManager").GetComponent<GameManager> (); 13 _lineKey = GameUtil.GetKeyCodeByLineNum(lineNum); 14 } 15 16 void Update () { 17 this.transform.position += Vector3.down * 10 * Time.deltaTime; 18 19 if (this.transform.position.y < -5.0f) { 20 Debug.Log ("false"); 21 Destroy (this.gameObject); 22 } 23 24 if(isInLine){ 25 CheckInput(_lineKey); 26 } 27 28 } 29 30 31 void OnTriggerEnter (Collider other) { 32 isInLine = true; 33 } 34 35 void OnTriggerExit (Collider other) { 36 isInLine = false; 37 } 38 39 void CheckInput (KeyCode key) { 40 if (Input.GetKeyDown (key)) { 41 _gameManager.GoodTimingFunc (lineNum); 42 Destroy (this.gameObject); 43 } 44 45 } 46}
ここではオブジェクトが生成されています。
オブジェクトの生成に関しては問題ないと思います
C#
1using UnityEngine; 2using System.Collections; 3using System.IO; 4using System; 5using UnityEngine.UI; 6 7public class GameManager : MonoBehaviour { 8 [Serializable] public class InputJson 9 { 10 public string name; 11 public int BPM; 12 public Note[] Notes; 13} 14[Serializable] public class Note 15 { 16 public int LPB; 17 public int num; 18 public int block; 19 public int type; 20} 21 public GameObject[] notes; 22 private float[] _timing; 23 private int[] _lineNum; 24 public string filePass; 25 private int _notesCount = 0; 26 public float timing; 27 private AudioSource _audioSource; 28 private float _startTime = 0; 29 public float timeOffset = 0; 30 private bool _isPlaying = false; 31 public GameObject startButton; 32 public Text scoreText; 33 private int _score = 0; 34 void Start(){ 35 _audioSource = GameObject.Find ("GameMusic").GetComponent<AudioSource> (); 36 _timing = new float[1024]; 37 _lineNum = new int[1024]; 38 LoadCSV (); 39 } 40 41 void Update () { 42 if (_isPlaying) { 43 CheckNextNotes (); 44 scoreText.text = _score.ToString (); 45 } 46 47 } 48 49 public void StartGame(){ 50 startButton.SetActive (false); 51 _startTime = Time.time; 52 _audioSource.Play (); 53 _isPlaying = true; 54 } 55 56 void CheckNextNotes(){ 57 while (_timing [_notesCount] + timeOffset < GetMusicTime () && _timing [_notesCount] != 0) { 58 SpawnNotes (_lineNum[_notesCount]); 59 _notesCount++; 60 61 } 62 } 63 64 void SpawnNotes(int block){ 65 if(block==0){ 66 Instantiate (notes[block], 67 new Vector3 (-5.3f, 10.0f, 0), 68 Quaternion.identity); 69 } 70 if(block==1){ 71 Instantiate (notes[block], 72 new Vector3 (-1.85f, 10.0f, 0), 73 Quaternion.identity); 74 } 75 if(block==2){ 76 Instantiate (notes[block], 77 new Vector3 (1.85f, 10.0f, 0), 78 Quaternion.identity); 79 } 80 if(block==3){ 81 Instantiate (notes[block], 82 new Vector3 (5.3f, 10.0f, 0), 83 Quaternion.identity); 84 } 85 } 86 87 void LoadCSV(){ 88 string json = Resources.Load (filePass).ToString(); 89 InputJson inputJson = JsonUtility.FromJson<InputJson>(json); 90 Debug.Log(inputJson.name); 91 int i=0; 92 for(int j=0;j < inputJson.Notes.Length; j++){ 93 timing=((float)inputJson.Notes[j].num*4+inputJson.Notes[j].LPB)/100; 94 _timing [i] = timing; 95 _lineNum [i] = inputJson.Notes[j].block; 96 i++; 97 } 98 } 99 float GetMusicTime(){ 100 return Time.time - _startTime; 101 } 102 public void GoodTimingFunc(int block){ 103 Debug.Log ("Line:" + block + " good!"); 104 Debug.Log (GetMusicTime()); 105 106 _score++; 107 } 108}
試したこと
参考にしている記事
[Unity]シンプルな音ゲーの作り方 part2 ゲーム製作
UnityでJSONファイルを読み込むメモ
補足情報(FW/ツールのバージョンなど)
Unity 2020.1.5f1
NoteEditor
【Unity】音ゲーの譜面エディタ「NoteEditor」紹介
あなたの回答
tips
プレビュー