Challenge
[Day 19] Suika Game Tutorial 3
GamJia
2024. 11. 25. 22:26
오블완 챌린지 19일차

주말에 본가 가느라 주말 기타
연습을 못해서 오늘은 빡연습
하고 와서 어깨 아픈 GamJia 입니다
어제 강의에 이어서
Suika Game을 만들어 보겠습니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SuikaGame;
namespace SuikaGame
{
public class Fruit : MonoBehaviour
{
...
public void Destroy()
{
Destroy(this.gameObject);
}
}
}
일단 어제 작업한 Fruit에
Destroy method를 만들겠습니다
Animation에 연결해서 재생이 끝나면
바로 Destroy 시키기 위해 만들었습니다
Animation이 끝나는 지점에
Event를 하나 생성해주고
방금 만들어준 Destroy() Event를
연결해주었습니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SuikaGame;
namespace SuikaGame
{
public class GameManager : MonoBehaviour
{
[SerializeField] private List<GameObject> fruitPrefabs; // 랜덤으로 생성할 과일 프리팹 리스트
private GameObject fruit; // 현재 드래그 중인 과일
public static GameManager Instance => instance;
private static GameManager instance;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
...
void CreateFruit()
{
int randomIndex = Random.Range(0, 4);
fruit = Instantiate(fruitPrefabs[randomIndex]);
fruit.name = fruitPrefabs[randomIndex].name; // 추가
fruit.transform.position = new Vector3(0f, 4f, 0f);
Rigidbody2D rb = fruit.GetComponent<Rigidbody2D>();
if (rb != null)
{
rb.isKinematic = true;
}
}
...
public void DestroyFruit(GameObject first, GameObject second) // 추가
{
if (!first.GetComponent<Fruit>().isMerged&&!second.GetComponent<Fruit>().isMerged)
{
return;
}
first.GetComponent<Animator>().SetTrigger("isDisappear");
second.GetComponent<Animator>().SetTrigger("isDisappear");
int index = -1;
for (int i = 0; i < fruitPrefabs.Count; i++)
{
if (fruitPrefabs[i].name == first.gameObject.name)
{
index = i;
break;
}
}
// 다음 레벨 과일 생성
if (index == -1 || index + 1 >= fruitPrefabs.Count)
{
return;
}
Vector3 spawnPosition = (first.transform.position + second.transform.position) / 2;
GameObject newFruit = Instantiate(fruitPrefabs[index + 1], spawnPosition, Quaternion.identity);
// 생성된 과일의 이름 설정
newFruit.name = fruitPrefabs[index + 1].name;
}
}
}
일단 수정, 추가된 부분은 다음과 같습니다
Method | 설명 |
CreateFruit(수정) | Instantiate 후 이름 뒤에 '(Clone)'이 붙지 않게 prefab의 이름으로 바꾸어 주었습니다 |
DestroyFruit(추가) | 두 개의 과일이 합쳐지면, 사라지는 애니메이션을 실행하고, 두 과일의 중간 위치에 새로운 레벨의 과일을 생성합니다. 과일이 합쳐진 상태에서만 작동하고, fruitPrefabs 배열에서 해당 과일의 다음 레벨을 찾아 생성합니다 |
새로 만든 객체 이름을 prefab 이름으로
바꿔줌으로써 다음 레벨의 객체가 뭔지
쉽게 찾을 수 있게 했습니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SuikaGame;
namespace SuikaGame
{
public class Fruit : MonoBehaviour
{
public bool isMerged=false;
private void OnCollisionEnter2D(Collision2D collision)
{
// 충돌한 객체에서 Fruit 컴포넌트를 가져옵니다.
Fruit otherFruit = collision.gameObject.GetComponent<Fruit>();
if (otherFruit != null) // Fruit 컴포넌트가 있는 경우
{
if(collision.gameObject.name==this.gameObject.name)
{
if(isMerged||otherFruit.isMerged)
{
return;
}
isMerged=true;
GameManager.Instance.DestroyFruit(this.gameObject,collision.gameObject);
}
}
}
public void Destroy()
{
Destroy(this.gameObject);
}
}
}
그리고 Fruit의 Animator 부분을
GameManager가 실행하게 해주었으며
isMerged라는 bool 값을 이용해
만약에 이게 true면 return 되게끔 해주었습니다
저장 후 실행해보니 어제랑 다르게
같은 객체랑 부딪혔을 때 사라지고
다음 레벨의 객체가 두 객체 사이
위치에 잘 생성된걸 확인했습니다!
오늘은 여기까지 하고
GitHub에 Commit하는걸로
마무리 하겠습니다!
내일은 회사 분들이랑
술먹기로 해서ㅋㅋ
간단한 점수 계산 방법
알려드리도록 하겠습니다!
감사합니다!!