unityでゲームをビルドした際にキーボード操作ができなくなる。

実現したいこと

ビルドしたゲームをキーボードで遊べるようにする。

発生している問題・分からないこと

時間を表示するUIなどのほかは動いているがキーボード操作だけができない。

該当のソースコード

C#

1sing System.Collections;2using System.Collections.Generic;3using UnityEngine;4using UnityEngine.UI;5 6public class PlayerPgm : MonoBehaviour 7{8 //コイン取得数9 private int coin;10 //コインテキスト11 public Text coinlabel;12 //ゴールテキスト13 public Text goallabel;14 //重力15 public Rigidbody2D rb2d;16 //ジャンプ力17 public float jumpForce = 5f;18 //移動の速さ19 public float workForce = 3f;20 21 // Start is called before the first frame update22 void Start()23 {24 coinlabel.text = "Coin:" + coin;25 goallabel.text = " ";26 }27 28 // Update is called once per frame29 void Update()30 {31 if (Input.GetKey(KeyCode.W))32 {33 rb2d.AddForce(Vector3.up * jumpForce);34 }35 36 if (Input.GetKey(KeyCode.A))37 {38 rb2d.AddForce(Vector3.left * workForce);39 }40 41 if (Input.GetKey(KeyCode.D))42 {43 rb2d.AddForce(Vector3.right * workForce);44 }45 46 if (Input.GetKey(KeyCode.S))47 {48 rb2d.AddForce(Vector3.down * 2);49 }50 51 if (Input.GetKey(KeyCode.R))52 {53 transform.position = new Vector3(-7, -4, 0);54 rb2d.velocity = Vector3.zero;55 }56 57 if (coin == 100)58 {59 goallabel.text = "YOU ARE PERFECT!!";60 }61 }62 63 private void OnTriggerEnter2D(Collider2D collision)64 {65 if (collision.CompareTag("Item"))66 {67 Destroy(collision.gameObject);68 coin++;69 coinlabel.text = "Coin:" + coin;70 }71 72 if (collision.CompareTag("Blue Coin"))73 {74 Destroy(collision.gameObject);75 coin += 10;76 coinlabel.text = "Coin:" + coin;77 }78 79 if (collision.CompareTag("An"))80 {81 transform.position = new Vector3(-7, -4, 0);82 rb2d.velocity = Vector3.zero;83 }84 85 if (collision.CompareTag("Goal"))86 {87 if (coin == 100)88 {89 goallabel.text = "GOAL!! YOU ARE PERFECT!!";90 }91 else92 {93 goallabel.text = "GOAL!!";94 }95 }96 }97}98

C#

1using System.Collections;2using System.Collections.Generic;3using UnityEngine;4using UnityEngine.UI;5 6public class TimerPgm : MonoBehaviour 7{8 float countTime = 0;9 10 // Start is called before the first frame update11 void Start()12 {13 14 }15 16 // Update is called once per frame17 void Update()18 {19 // countTimeに、ゲームが開始してからの秒数を格納20 countTime += Time.deltaTime;21 22 // 小数2桁にして表示23 GetComponent<Text>().text = countTime.ToString("F2");24 }25}

試したこと・調べたこと

上記の詳細・結果

teratailやCopilotで調べましたが、出来ませんでした。

補足

このゲームはWASDキーで操作し、コインを100枚集めるとクリアするゲームです。

コメントを投稿

0 コメント