UnityでC#で記述していますズブの初心者です。
ボタンを押している間オブジェクトがY軸に動くスクリプトを作成中どうしてもboolで指定した「PushUpKey」が毎フレーム「false」にされてしまいます。
ボタンを押した瞬間「true」にはなるのですが次のフレームになった瞬間「false」にされてしまいボタンを押しても動いてくれません。
初期値を「true」にした場合は今度は動きっぱなしになってしまい困っています。
void Updateには「PushUpKey」を変化させるものを入れていないのになんでこんなことになるのかわかりません。
もしわかる方いましたらご教授いただければと思います。よろしくお願いいたします。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Player : MonoBehaviour { float ValueY; bool PushUpKey; Spaceship Spaceship; IEnumerator Start(){ Spaceship = GetComponent<Spaceship>(); while (true){ Spaceship.Shot(transform); GetComponent<AudioSource>().Play(); yield return new WaitForSeconds(Spaceship.ShotDelay); } } void Move(Vector2 direction) { Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); Vector2 pos = transform.position; pos += direction * Spaceship.Speed * Time.deltaTime; pos.x = Mathf.Clamp(pos.x, min.x, max.x); pos.y = Mathf.Clamp(pos.y, min.y, max.y); transform.position = pos; } void Update() { if (PushUpKey == true){ ValueY = 1; Debug.Log("1111");//原因特定用 } else if (PushUpKey == false) { ValueY = 0; Debug.Log("0000");//原因特定用 } Vector2 direction = new Vector2(0, ValueY).normalized; Move(direction); } public void UpKeyDown()//ボタンを押している間 { PushUpKey = true; Debug.Log(PushUpKey);//原因特定用 } public void UpKeyUp()//ボタンを離したとき { PushUpKey = false; Debug.Log(PushUpKey);//原因特定用 } }
UpKeyDownメソッドとUpKeyUpメソッドはUNITY上でボタン押下時に実行するよう指定しています。
回答1件
あなたの回答
tips
プレビュー