- 해적 고양이의 속성
- Scale : x:0.8, y:0.8
- speed : 0.1f
- Lv.4에서 해적 고양이 등장시키기
- 조건문 Lv.4 이상 추가
- NormalCat은 50% 확률로 등장
- FatCat, PirateCat 등장
결과
정답코드
더보기
Cat.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cat : MonoBehaviour
{
public GameObject hungryCat;
public GameObject fullCat;
public RectTransform front;
public int type;
float full = 5.0f; // 최대 체력
float energy = 0.0f; // 현재 체력
float speed = 0.05f;
bool isFull = false;
// Start is called before the first frame update
void Start()
{
// 고양이 랜덤 위치 생성
float x = Random.Range(-9.0f, 9.0f);
float y = 30.0f;
transform.position = new Vector2(x, y);
if(type == 1) // NormalCat
{
speed = 0.05f;
full = 5f;
}
else if(type == 2) // FullCat
{
speed = 0.02f;
full = 10f;
}
else if (type == 3) // PirateCat
{
speed = 0.1f;
}
}
// Update is called once per frame
void Update()
{
if(energy < full) // 현재 체력 < 전체 체력일 때 아래로 내려가기
{
transform.position += Vector3.down * speed;
// 배부르지 않은 고양이가 생선가게에 닿았을 때
if(transform.position.y < -16.0f)
{
GameManager.Instance.GameOver(); // 게임 종료
}
}
else // 체력바가 다 찬 상태일 때 옆으로 이동하기(왼쪽일 땐 왼쪽으로, 오른쪽일 땐 오른쪽으로 이동)
{
// 중앙 기준으로 오른쪽에 있을 때
if(transform.position.x > 0)
{
transform.position += Vector3.right * 0.05f; // 오른쪽으로 이동
}
else // x가 0이거나 x(중앙)보다 작을 때
{
transform.position += Vector3.left * 0.05f; // 왼쪽으로 이동
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Food"))
{
if(energy < full) // 현재 체력 < 전체 체력
{
// 체력바 게이지 상승
energy += 1.0f;
front.localScale = new Vector3(energy / full, 1.0f, 1.0f); // 현재 체력을 최대 체력으로 나누어서 비율을 계산
// 고양이에게 맞은 Food는 파괴
Destroy(collision.gameObject);
// 게이지가 다 차고 바로 fullCat으로 바뀌게 하기 위해서 여기에 코드 작성
if (energy == 5.0f) // 체력바가 다 찬 상태
{
// 점수를 더할 때 배가 부른 고양이가 Food에 또 맞게 되면 발생하는 문제 방어
if (!isFull)
{
isFull = true;
hungryCat.SetActive(false);
fullCat.SetActive(true);
Destroy(gameObject, 3.0f); // 3초 후 고양이 파괴
GameManager.Instance.AddScore();
}
}
}
}
}
}
GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public GameObject normalCat;
public GameObject fatCat;
public GameObject pirateCat;
public GameObject retryBtn;
public RectTransform levelFront;
public Text levelTxt;
int level = 0; // 현재 레벨
int score = 0; // 점수 5점당 1레벨
private void Awake()
{
if(Instance == null)
{
Instance = this;
}
Application.targetFrameRate = 60;
Time.timeScale = 1f;
}
// Start is called before the first frame update
void Start()
{
InvokeRepeating("MakeCat", 0f, 1f);
}
void MakeCat()
{
Instantiate(normalCat);
if(level == 1)
{
// lv.1 20% 확률로 고양이를 더 생성해준다.
int p = Random.Range(0, 10); // 0 ~ 9
if(p < 2) Instantiate(normalCat); // 10개중에 2개만 선택됨 => 20% 확률 표현
}
else if(level == 2)
{
// lv.2 50% 확률로 고양이를 더 생성해준다.
int p = Random.Range(0, 10); // 0 ~ 9
if (p < 5) Instantiate(normalCat); // 10개중에 5개만 선택됨 => 50% 확률 표현
}
else if(level == 3)
{
// lv.3 뚱뚱한 고양이를 생성해준다.
Instantiate(fatCat);
}
else if (level >= 4)
{
// lv.4 해적 고양이를 생성해준다.
int p = Random.Range(0, 10);
if (p < 5) Instantiate(normalCat);
Instantiate(fatCat);
Instantiate(pirateCat);
}
}
public void GameOver()
{
retryBtn.SetActive(true);
Time.timeScale = 0f;
}
public void AddScore()
{
score++;
level = score / 5; // 0~4는 0레벨, 5~9는 1레벨 이런식으로 몫을 구함
levelTxt.text = level.ToString();
levelFront.localScale = new Vector3((score - level * 5) / 5.0f, 1f, 1f); // ex. 점수가 12면 12 - 10 = 2가 됨
// 소수점값을 얻기 위해 5.0f(실수값)으로 나누어줌
}
}
반응형
'스파르타 게임개발종합반(Unity) > 사전캠프 공부 기록' 카테고리의 다른 글
[Unity] 부모 오브젝트 안에 자식 프리팹 생성하기 - Prefabs, Instantiate (0) | 2024.04.05 |
---|---|
[Unity/2D] 텍스처 이미지 크기(사이즈) 조절 - Pixels Per Unit (0) | 2024.04.05 |
[Unity] 프리팹 인스턴스 언패킹 - Unpack (0) | 2024.04.04 |
[Unity] Prefabs 확률로 생성하기, 게임 규칙 추가하기 (0) | 2024.04.04 |
[Unity/2D] 레벨 시스템 구현 - UI, text, ToString, localScale (0) | 2024.04.04 |
댓글