質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.41%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

1033閲覧

ゲームクリア後もプレイヤーが動いてしまう

Uxox

総合スコア3

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2022/12/01 05:26

前提

Unitiy2Dの教科書を参考にしながら、強制横スクロールの中、音声入力でジャンプをし、ゴールを目指すゲームを作っています。

実現したいこと

プレイヤーがGoalタグ又はDeadタグに触れると、ゲームが停止してプレイヤーが動かなくなるようにしたいです。

発生している問題・エラーメッセージ

プレイヤーがGoalタグに触れた後にゲームクリア画面が出るのですが、ゲームが停止しないため、クリア画面の後ろでプレイヤーが移動してしまい、Deadタグに触れてゲームオーバーの画面が出てきます。

該当のソースコード

using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using System; public class PlayerController : MonoBehaviour { Rigidbody2D rbody2D; float axisH = 0.0f; public float jumpForce = 350f; public int jumpCount = 0; bool onGround = false; public static string gameState = "playing";//ゲームの状態 public int score = 0; Animator animator; public string stopAnime = "PlayerStop"; public string moveAnime = "PlayerMove"; public string jumpAnime = "PlayerJump"; public string goalAnime = "PlayerGoal"; public string deadAnime = "PlayerDead"; string nowAnime = ""; string oldAnime = ""; ///////////////////////////////////// [SerializeField] private string m_DeviceName; private AudioClip m_AudioClip; private int m_LastAudioPos; private float m_AudioLevel; [SerializeField] private GameObject m_Cube; [SerializeField, Range(10, 100)] private float m_AmpGain = 10; void Start() { rbody2D = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); nowAnime = stopAnime; oldAnime = stopAnime; gameState = "playing"; string targetDevice = ""; foreach (var device in Microphone.devices) { Debug.Log($"Device Name: {device}"); if (device.Contains(m_DeviceName)) { targetDevice = device; } } Debug.Log($"=== Device Set: {targetDevice} ==="); m_AudioClip = Microphone.Start(targetDevice, true, 10, 48000); } void Update() { axisH = Input.GetAxisRaw("Horizontal"); float[] waveData = GetUpdatedAudio(); if (waveData.Length == 0) return; m_AudioLevel = waveData.Average(Mathf.Abs); if (m_AudioLevel > 0.02 && this.jumpCount < 2) { Debug.Log("jump"); this.rbody2D.AddForce(transform.up * jumpForce); jumpCount++; } if (gameState != "playing") { return; } } void FixedUpdate() { if (gameState != "playing") { return; } if (onGround) { if (axisH == 0) { nowAnime = stopAnime; } else { nowAnime = moveAnime; } } else { nowAnime = jumpAnime; } if (nowAnime != oldAnime) { oldAnime = nowAnime; animator.Play(nowAnime); } } void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.CompareTag("Ground")) { jumpCount = 0; } } void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "ScoreItem") { ItemData item = collision.gameObject.GetComponent<ItemData>(); score = item.value; Destroy(collision.gameObject); } else if (collision.gameObject.tag == "Goal") { Debug.Log("Goal"); Goal(); } else if (collision.gameObject.tag == "Dead") { Debug.Log("dead"); GameOver(); } } public void Goal() { animator.Play(goalAnime); gameState = "gameClear"; GameStop(); } public void GameOver() { animator.Play(deadAnime); gameState = "gameover"; GameStop(); } private float[] GetUpdatedAudio() { int nowAudioPos = Microphone.GetPosition(null);// nullでデフォルトデバイス float[] waveData = Array.Empty<float>(); if (m_LastAudioPos < nowAudioPos) { int audioCount = nowAudioPos - m_LastAudioPos; waveData = new float[audioCount]; m_AudioClip.GetData(waveData, m_LastAudioPos); } else if (m_LastAudioPos > nowAudioPos) { int audioBuffer = m_AudioClip.samples * m_AudioClip.channels; int audioCount = audioBuffer - m_LastAudioPos; float[] wave1 = new float[audioCount]; m_AudioClip.GetData(wave1, m_LastAudioPos); float[] wave2 = new float[nowAudioPos]; if (nowAudioPos != 0) { m_AudioClip.GetData(wave2, 0); } waveData = new float[audioCount + nowAudioPos]; wave1.CopyTo(waveData, 0); wave2.CopyTo(waveData, audioCount); } m_LastAudioPos = nowAudioPos; return waveData; } void GameStop() { Rigidbody2D rbody2D = GetComponent<Rigidbody2D>(); rbody2D.velocity = new Vector2(0, 0); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameManager : MonoBehaviour { public GameObject mainImage; public Sprite gameOverSpr; public Sprite gameClearSpr; public GameObject Panel; public GameObject restartButton; public GameObject nextButton; Image titleImage; //+++スコア追加+++ public GameObject scoreText; //スコアテキスト public static int totalScore; //合計スコア public int stageScore = 0; //ステージスコア // Start is called before the first frame update void Start() { Invoke("InactiveImage", 0.0f); Panel.SetActive(false); UpdateScore(); } // Update is called once per frame void Update() { if (PlayerController.gameState == "gameClear") { mainImage.SetActive(true); Panel.SetActive(true); mainImage.GetComponent<Image>().sprite = gameClearSpr; PlayerController.gameState = "gameend"; } else if(PlayerController.gameState=="gameover") { mainImage.SetActive(true); Panel.SetActive(true); Button bt = nextButton.GetComponent<Button>(); bt.interactable = false; mainImage.GetComponent<Image>().sprite = gameOverSpr; PlayerController.gameState = "gameend"; } else if (PlayerController.gameState == "playing") { //ゲーム中 GameObject player = GameObject.FindGameObjectWithTag("Player"); PlayerController playerCnt = player.GetComponent<PlayerController>(); if (playerCnt.score != 0) { stageScore += playerCnt.score; playerCnt.score = 0; UpdateScore(); } } } void InactiveImage() { mainImage.SetActive(false); } void UpdateScore() { int score = stageScore + totalScore; scoreText.GetComponent<Text>().text = score.ToString(); } }

試したこと

GameManagerスクリプト内の"gameend"は、PlayerControllerスクリプトから参照しているようですが、
PlayerControllerスクリプト内には無いので、GameStop()内に、gamestate="gameend"を設定してみました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

GameOver()内でプレイヤーの当たりを消すようにしたら出来ました!!

投稿2022/12/01 06:06

Uxox

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.41%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問