мне надо создать игру про космический шутер ,и вот мне надо как то сделать что бы при столкновении с метеоритами или врагами отнимались хп либо у игрока либо у базы
public class GameManager : MonoBehaviour { [SerializeField] Text txtStatistic, txtFinish; [SerializeField] Player Player; [SerializeField] Transform basePlayerTransform; [SerializeField] BasePlayer basePlayer; [SerializeField] GameObject meteorite, enemy, menuPause, menuFinish; [SerializeField] int baseHp, playerHp, countEnemys, countMeteorites; GameObject[] enemys;
// Start is called before the first frame update void Start() { GenerateLevel(); Player.hp = playerHp; basePlayer.hp = baseHp; Time.timeScale = 1; Cursor.lockState = CursorLockMode.Locked; }
int getCountEnemy() { int count = 0; foreach (GameObject g in enemys) if (g != null) count++; return count; }
void Update() { txtStatistic.text = $"♥{Player.hp,3}\n⌂{basePlayer.hp,3}\n☠{getCountEnemy(),3}"; if (basePlayer.hp <= 0 || Player.hp == 0) { Lose(); return; } if (getCountEnemy() == 0) { Win(); return; } if (Input.GetKeyDown(KeyCode.Escape)) PauseGame();
} void GenerateLevel() { enemys = new GameObject[countEnemys];
public class GameManager : MonoBehaviour
{
[SerializeField] Text txtStatistic, txtFinish;
[SerializeField] Player Player;
[SerializeField] Transform basePlayerTransform;
[SerializeField] BasePlayer basePlayer;
[SerializeField] GameObject meteorite, enemy, menuPause, menuFinish;
[SerializeField] int baseHp, playerHp, countEnemys, countMeteorites;
GameObject[] enemys;
// Start is called before the first frame update
void Start()
{
GenerateLevel();
Player.hp = playerHp;
basePlayer.hp = baseHp;
Time.timeScale = 1;
Cursor.lockState = CursorLockMode.Locked;
}
int getCountEnemy()
{
int count = 0;
foreach (GameObject g in enemys)
if (g != null)
count++;
return count;
}
void Update()
{
txtStatistic.text = $"♥{Player.hp,3}\n⌂{basePlayer.hp,3}\n☠{getCountEnemy(),3}";
if (basePlayer.hp <= 0 || Player.hp == 0)
{
Lose();
return;
}
if (getCountEnemy() == 0)
{
Win();
return;
}
if (Input.GetKeyDown(KeyCode.Escape))
PauseGame();
}
void GenerateLevel()
{
enemys = new GameObject[countEnemys];
for (int i = 0; i < countEnemys; i++)
{
Vector3 randomPos = Random.onUnitSphere * 15000;
enemys[i] = Instantiate(enemy, randomPos, Quaternion.identity);
Enemy script = enemys[i].GetComponent<Enemy>();
script.target = basePlayerTransform;
}
for (int i = 0; i < countMeteorites; i++)
{
Vector3 randomPos = Random.onUnitSphere * 30000;
Instantiate(meteorite, randomPos, Quaternion.identity);
}
}
void Lose()
{
menuFinish.SetActive(true);
txtFinish.text = "Поражение";
Time.timeScale = 0;
Cursor.lockState = CursorLockMode.None;
}
void Win()
{
menuFinish.SetActive(true);
txtFinish.text = "Победа";
Time.timeScale = 0;
Cursor.lockState = CursorLockMode.None;
}
void PauseGame()
{
menuPause.SetActive(true);
Time.timeScale = 0f;
Cursor.lockState = CursorLockMode.None;
}
public void ContinueGame()
{
menuPause.SetActive(false);
Time.timeScale = 1;
Cursor.lockState = CursorLockMode.Locked;
}
public void RestartGame()
{
SceneManager.LoadScene(1);
}
public void Exit()
{
SceneManager.LoadScene(0);
}
}