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