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

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

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

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

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

599閲覧

ボーナスを実装したい

fxi1145

総合スコア0

C#

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

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2022/10/29 05:37

編集2022/10/29 06:46

前提

unityでシューティングゲームを作っています。

実現したいこと

敵の撃破数によるボーナスと生存時間によるボーナスを実装したいです。

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

生存時間によるボーナスが無限に入っているときがある
撃破数によるボーナスが入らない

該当のソースコード

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { private int score = 0; private int defeat = 0; private float minute = 0; private float seconds = 0; private void Awake() { DontDestroyOnLoad(this.gameObject); } public int EnemyScore { get { return this.score;} set { this.score = value;} } public int EnemyDefeat { get { return this.defeat; } set { this.defeat = value; } } public float TimeMinute { get { return this.minute;} set { this.minute = value;} } public float TimeSeconds { get { return this.seconds;} set { this.seconds = value;} } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } } /*public void ScoreMas() { if (defeat == 10) { ScoreMas(); } if (defeat > 10 && defeat / 10 == 0) { score += 50; } switch(defeat) { case 1:if (defeat == 10) Debug.Log("a"); score += 100; break; case 2:if (defeat > 10 && defeat / 10 == 0) score += 50; break; } }*/
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class TimeCount : MonoBehaviour { // 前のUpdateの時の秒数 private float oldSeconds; // タイマー表示用テキスト private Text timerText; private float timeLimit = 1000.0f; private float countup = 0.0f; private ScoreManager scoreManager; void Start() { oldSeconds = 0f; timerText = GetComponentInChildren<Text>(); this.scoreManager = FindObjectOfType<ScoreManager>(); } void Update() { scoreManager.TimeSeconds += Time.deltaTime; if (scoreManager.TimeSeconds >= 60f) { scoreManager.TimeMinute++; scoreManager.TimeSeconds = scoreManager.TimeSeconds % 60; } // 値が変わった時だけテキストUIを更新 if(scoreManager.TimeMinute / 60 == 0) { Debug.Log("score"); scoreManager.EnemyScore += 50; } oldSeconds = scoreManager.TimeSeconds; GameClear(); void GameClear() { countup += Time.deltaTime; if (countup >= timeLimit) { SceneManager.LoadScene("GameClear"); scoreManager.EnemyScore += 1000; } } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Enemy : MonoBehaviour { public float hp; public float atk; [SerializeField] GameObject a; private Shooting shooting; private ScoreManager scoreManager; public float Atk { get { return this.atk;} set { this.atk = value;} } //public int defeatValue; // Start is called before the first frame update void Start() { this.shooting = FindObjectOfType<Shooting>(); this.scoreManager = FindObjectOfType<ScoreManager>(); } // Update is called once per frame void Update() { } private void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Bullet") { Debug.Log("hit Player"); hp = hp - shooting.ardamage; if(hp <= 0) { Destroy(this.gameObject); Instantiate(a, transform.position, Quaternion.identity); Debug.Log("1"); scoreManager.EnemyScore += 25; scoreManager.EnemyDefeat += 1; } } if (other.gameObject.tag == "HgBullet") { Debug.Log("hit Player"); hp = hp - shooting.hgdamage; if (hp <= 0) { Destroy(this.gameObject); Instantiate(a, transform.position, Quaternion.identity); scoreManager.EnemyScore += 100; scoreManager.EnemyDefeat += 1; } } } }

試したこと

switch文,if文などでループさせて一定間隔でボーナスを入れようとした

if(scoreManager.TimeMinute / 60 == 0) { Debug.Log("score"); scoreManager.EnemyScore += 50; } /*public void ScoreMas() { if (defeat == 10) { ScoreMas(); } if (defeat > 10 && defeat / 10 == 0) { score += 50; } switch(defeat) { case 1:if (defeat == 10) Debug.Log("a"); score += 100; break; case 2:if (defeat > 10 && defeat / 10 == 0) score += 50; break; } }*/

補足情報(FW/ツールのバージョンなど)

unity2020.3.26f1

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

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

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

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

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

ganta7188

2022/10/29 07:41

・defeat == 10のときにScoreMas()が無限に呼ばれませんか? ・scoreManager.TimeMinute / 60 == 0 これだと0分、60分、120分などの間はずっとスコア加算されると思います。ただTimeSecondsに直したとしても1秒間に30回呼ばれる(30fpsの場合)ので、分や秒じゃなくframeCountを使うほうが良いかと思います
guest

回答1

0

すみません、コメントに書いたのですが、たぶんほぼ回答だと思うのでこちらにも書きます。

・defeat == 10のときにScoreMas()が無限に呼ばれませんか?
・scoreManager.TimeMinute / 60 == 0 これだと0分、60分、120分などの間はずっとスコア加算されると思います。ただTimeSecondsに直したとしても1秒間に30回呼ばれる(30fpsの場合)ので、分や秒じゃなくframeCountを使うほうが良いかと思います

投稿2022/10/29 07:43

ganta7188

総合スコア436

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

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

fxi1145

2022/10/29 07:45

そうなんですよね。なのでそこの部分はコメントアウトで今は使ってないです
ganta7188

2022/10/29 08:18

修正してコメントアウト外せば解決ではないのですか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問