Unity Junior Course - Click Mouse(Prototype5)

GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public List<GameObject> targets;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI gameOverText;
public Button restartButton;
public GameObject titleScreen;
public bool isGameActive;
private int score;
private float spawnRate = 1.0f;
void Start()
{
}
void Update()
{
}
IEnumerator SpawnTarget()
{
while (isGameActive)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targets.Count);
Instantiate(targets[index]);
}
}
public void UpdateScore(int scoreToAdd)
{
score += scoreToAdd;
scoreText.text = "Score: " + score;
}
public void GameOver()
{
restartButton.gameObject.SetActive(true);
gameOverText.gameObject.SetActive(true);
isGameActive = false;
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void StartGame(int difficulty)
{
isGameActive = true;
score = 0;
spawnRate /= difficulty;
StartCoroutine(SpawnTarget());
UpdateScore(0);
titleScreen.gameObject.SetActive(false); / 난이도 선택 화면 비활성화
}
}
Target.cs
using UnityEngine;
public class Target : MonoBehaviour
{
private Rigidbody targetRb;
private GameManager gameManager;
private float minSpeed = 12;
private float maxSpeed = 16;
private float maxTorque = 10;
private float xRange = 4;
private float ySpawnPos = -6;
public ParticleSystem explosionParticle;
public int pointValue;
void Start()
{
targetRb = GetComponent<Rigidbody>();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
targetRb.AddForce(RandomForce(), ForceMode.Impulse);
targetRb.AddTorque(RandomTorque(), RandomTorque(), RandomTorque(), ForceMode.Impulse);
transform.position = RandomSpawnPos();
}
void Update()
{
}
private void OnMouseDown()
{
if (gameManager.isGameActive)
{
gameManager.UpdateScore(pointValue);
Destroy(gameObject);
Instantiate(explosionParticle, transform.position, explosionParticle.transform.rotation);
}
}
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
if(!gameObject.CompareTag("Bad"))
{
gameManager.GameOver();
}
}
Vector3 RandomForce()
{
return Vector3.up * Random.Range(minSpeed, maxSpeed);
}
float RandomTorque()
{
return Random.Range(-maxTorque, maxTorque);
}
Vector3 RandomSpawnPos()
{
return new Vector3(Random.Range(-xRange, xRange), ySpawnPos);
}
}
using UnityEngine;
using UnityEngine.UI;
public class DifficultyButton : MonoBehaviour
{
private Button button;
private GameManager gameManager;
public int difficulty;
void Start()
{
button = GetComponent<Button>();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
button.onClick.AddListener(SetDifficulty);
}
void Update()
{
}
void SetDifficulty()
{
Debug.Log(gameObject.name + " was clicked");
gameManager.StartGame(difficulty);
}
}

