동전 먹기
오브젝트 생성 후 각각의 Item에 이름을 붙여준다.
collider에서 isTrigger 체크하기
player 스크립트
public GameManager gm; //GameManager 오브젝트 맵핑하기
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Item")
{
//Point
bool isBronze = collision.gameObject.name.Contains("Bronze");//브론즈 코인
bool isSilver = collision.gameObject.name.Contains("Silver"); //실버 코인
bool isGold = collision.gameObject.name.Contains("Gold"); //골드 코인
if (isBronze) {
gm.stagePoint += 50; //GameManager에 접근가능
}
if (isSilver)
{
gm.stagePoint += 100;
}
if (isGold)
{
gm.stagePoint += 300;
}
//Deactive Item
collision.gameObject.SetActive(false); //오브젝트 비 활성화
}else if(collision.gameObject.tag == "Finish")
{
//Next Stage
playSound("Finish");
gm.nextStage();
}
public void velocityZero()
{
rigid.velocity = Vector2.zero;
}
}
게임 전반을 관리할 게임 매니저 생성
하이라키에서 마우스 우클릭 -> Create Empty
콜라이더와 스크립트 주기
public PlayerMove player; //플레이어 오브젝트 맵핑하기
public class GameManager : MonoBehaviour
{
public int totalPoint; //총 점수
public int stagePoint; //현재 스테이지 점수
public int stageIndex; //현재 스테이지 번호(스테이지는 총 3스테이지)
public int health; //목숨
public void nextStage()
{
if (stageIndex < 2) //1, 2스테이지의 경우 다음 스테이지 불러오기
{
//Next Stage
stages[stageIndex].SetActive(false);
stageIndex++;
stages[stageIndex].SetActive(true);
playerRePosition();
uiStage.text = "STAGE " + (stageIndex + 1);
totalPoint += stagePoint;
stagePoint = 0;
}
else { //3스테지까지 클리어시
Time.timeScale = 0;
Debug.Log("게임 클리어!");
}
}
}
void playerRePosition() { //플레이어 시작지점 재배치
player.transform.position = new Vector3(0, 0);
player.velocityZero(); //Player에 접근가능, 낙하속도를 초기화(낙사 시 속력이 빠른채로 위치 초기화되기 때문)
}
'유니티 2D 기본(Platformer Game)' 카테고리의 다른 글
10. 사운드 출력 (0) | 2021.12.04 |
---|---|
9. 스테이지 추가 & UI (0) | 2021.12.04 |
7. 몬스터 밟아서 잡기 (0) | 2021.12.03 |
6. 플레이어 피격 이벤트 구현 (0) | 2021.11.30 |
5. 몬스터 AI 구현하기 (0) | 2021.11.30 |