ボタンを押したときにbool型の変数shootbuttonをtrueにしたいです。
c#
1using UnityEngine; 2using System.Collections; 3using UnityStandardAssets.CrossPlatformInput; 4 5namespace CompletedAssets 6{ 7 public class Player : MonoBehaviour 8 { 9 // Spaceshipコンポーネント 10 Spaceship spaceship; 11 12 // 弾の連射用 13 private float timeCount1; 14//----------------------------------------------------- 15// 見てほしいところ1 ここから 16//----------------------------------------------------- 17 // 発射ボタン判定 18 bool shootbutton; 19 20 public void ShootButton() 21 { 22 Debug.Log("ShootButton"); 23 shootbutton = true; 24 } 25 26 public void noShootButton() 27 { 28 Debug.Log("noShootButton"); 29 shootbutton = false; 30 } 31//----------------------------------------------------- 32// 読んでほしいところ1 ここまで 33//----------------------------------------------------- 34 IEnumerator Start () 35 { 36 // Spaceshipコンポーネントを取得 37 spaceship = GetComponent<Spaceship> (); 38 yield return new WaitForSeconds(spaceship.shotDelay); 39 } 40 41 IEnumerator GUN() 42 { 43 // 弾をプレイヤーと同じ位置/角度で作成 44 spaceship.Shot(transform); 45 46 // ショット音を鳴らす 47 GetComponent<AudioSource>().Play(); 48 49 // shotDelay秒待つ 50 yield return new WaitForSeconds(spaceship.shotDelay); 51 } 52//----------------------------------------------------- 53// 見てほしいところ2 ここから 54//----------------------------------------------------- 55 void Update () 56 { 57 if (shootbutton) 58 { 59 Debug.Log("Shoot!"); 60 } 61//----------------------------------------------------- 62// 見てほしいところ2 ここまで 63//----------------------------------------------------- 64 // 右・左 65 float x = CrossPlatformInputManager.GetAxisRaw("Horizontal"); 66 67 // 上・下 68 float y = CrossPlatformInputManager.GetAxisRaw("Vertical"); 69 70 // 移動する向きを求める 71 Vector2 direction = new Vector2(x, y).normalized; 72 73 // 移動の制限 74 Move (direction); 75 76 if (Input.GetKey(KeyCode.Z) || shootbutton) 77 { 78 timeCount1++; 79 if (timeCount1 > spaceship.shotDelay * 10) 80 { 81 timeCount1 = 0; 82 StartCoroutine("GUN"); 83 } 84 } 85 else 86 { 87 timeCount1 = spaceship.shotDelay * 10; 88 } 89 90 } 91 92 // 機体の移動 93 void Move (Vector2 direction) 94 { 95 96 // 背景左下の座標を取得 97 Vector3 min = bg.GetComponent<Renderer>().bounds.min; 98 99 // 背景右上の座標を取得 100 Vector3 max = bg.GetComponent<Renderer>().bounds.max; 101 102 // プレイヤーの座標を取得 103 Vector2 pos = transform.position; 104 105 // 移動量を加える 106 pos += direction * spaceship.speed * Time.deltaTime; 107 108 // プレイヤーの位置が画面内に収まるように制限をかける 109 pos.x = Mathf.Clamp (pos.x, min.x + 0.28f, max.x - 0.32f); 110 pos.y = Mathf.Clamp (pos.y, min.y, max.y); 111 112 // 制限をかけた値をプレイヤーの位置とする 113 transform.position = pos; 114 } 115 116 // ぶつかった瞬間に呼び出される 117 void OnTriggerEnter2D (Collider2D c) 118 { 119 // レイヤー名を取得 120 string layerName = LayerMask.LayerToName (c.gameObject.layer); 121 122 // レイヤー名がBullet (Enemy)の時は弾を削除 123 if (layerName == "Bullet (Enemy)") { 124 // 弾の削除 125 Destroy (c.gameObject); 126 } 127 128 // レイヤー名がBullet (Enemy)またはEnemyの場合は爆発 129 if (layerName == "Bullet (Enemy)" || layerName == "Enemy") { 130 131 // Managerスクリプトからコンティニューコルーチンを呼び出す 132 FindObjectOfType<Manager>().Continue(); 133 134 // 爆発する 135 spaceship.Explosion (); 136 137 // プレイヤーを削除 138 Destroy (gameObject); 139 } 140 } 141 } 142}
デバッグしてみたときは表示されてましたが、trueに代入しようとしてもshootbuttonはtrueになりませんでした。
![
なおUpdateメソッド内ではshootbuttonをtrueに代入できました。ボタンは反応しているはずだと思いましたが、なぜshootbuttonに代入されないのかわかりません。
Unityのバージョンは2017.4.10f1 Personal(64bit)です。
追記
言葉足らずで申し訳ないです。
早速public bool shootbuttonと宣言し、Inspectorで見たところ、shootbuttonの値がボタンを押してる間はtrueになり、離したときにfalseになっていたことを確認できました。
「shootbuttonがtrueにならないことをどうやって確認したのか?」についてですが、
私は最初にUpdateメソッド内で
Debug.Log(shootbutton);
とした時にコンソールではTrueは表示されず、Falseしか表示されないことを確認しました。
※画像ではわかりやすくするために
Debug.Log(("shootbuttonは")+shootbutton);
としました。
c#
1void Update () 2 { 3 Debug.Log(("shootbuttonは")+shootbutton); 4 if (shootbutton) 5 { 6 Debug.Log("Shoot!"); 7 } 8 // 右・左 9 float x = CrossPlatformInputManager.GetAxisRaw("Horizontal"); 10 11 // 上・下 12 float y = CrossPlatformInputManager.GetAxisRaw("Vertical"); 13 14 // 移動する向きを求める 15 Vector2 direction = new Vector2(x, y).normalized; 16 17 // 移動の制限 18 Move (direction); 19 20 if (Input.GetKey(KeyCode.Z) || shootbutton) 21 { 22 timeCount1++; 23 if (timeCount1 > spaceship.shotDelay * 10) 24 { 25 timeCount1 = 0; 26 StartCoroutine("GUN"); 27 } 28 } 29 else 30 { 31 timeCount1 = spaceship.shotDelay * 10; 32 } 33 34 }
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/17 13:26