[Unity] 2D 엔드리스 러너 게임 만들기 (2) - 장애물

Kim Yuhyeon·2022년 12월 11일
0

게임개발

목록 보기
69/135

참고

초간단 유니티로 엔드리스 러너 만들기!

개발 환경

MacOS
Unity 2020.3.9f1


장애물

이동

MobBase.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MobBase : MonoBehaviour
{
    public float mobSpeed = 0;

    void Update()
    {
        transform.Translate(Vector2.left * Time.deltaTime * mobSpeed);
    }
}

결과물

충돌

  • Capsule Colider2D 컴포넌트 추가

랜덤으로 장애물 생성하기

  • Mob 프리팹으로 만들기

RespawnManager.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Random = UnityEngine.Random;

public class RespawnManager : MonoBehaviour
{
    public List<GameObject> MobPool = new List<GameObject>();
    public GameObject[] Mobs;
    public int objCnt = 1;
    private void Awake()
    {
        for (int i = 0; i < Mobs.Length; i++)
        {
            for (int q = 0; q < objCnt; q++)
            {
                MobPool.Add(CreateObj(Mobs[i], transform ));
            }
        }
    }

    private void Start()
    {
        StartCoroutine(CreateMob());
    }

    IEnumerator CreateMob()
    {
        while (true)
        {
            MobPool[Random.Range(0, MobPool.Count)].SetActive(true);
            yield return new WaitForSeconds(Random.Range(1f, 3f));
        }
    }

    GameObject CreateObj(GameObject obj, Transform parent)
    {
        GameObject copy = Instantiate(obj);
        copy.transform.SetParent(parent);
        copy.SetActive(false);
        return copy;
    }
}
  • 미리 얼만큼 장애물을 생성할지 정하고
  • 랜덤으로 활성화

결과물

몹 프리팹이 활성화되면 우측에서 시작 -> 좌측 넘어가면 비활성화

MobBase.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MobBase : MonoBehaviour
{
    public float mobSpeed = 0;
    private Vector2 startPosition;

    private void OnEnable()
    {
        transform.position = startPosition;
    }

    void Update()
    { 
        transform.Translate(Vector2.left * Time.deltaTime * mobSpeed);

        if (transform.position.x < -6)
        {
            gameObject.SetActive(false);
        }
    }
}

결과물

이미 활성화 된 것을 다시 활성화하는 로직 오류 수정

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Random = UnityEngine.Random;

public class RespawnManager : MonoBehaviour
{
    public List<GameObject> MobPool = new List<GameObject>();
    public GameObject[] Mobs;
    public int objCnt = 1;
    private void Awake()
    {
        for (int i = 0; i < Mobs.Length; i++)
        {
            for (int q = 0; q < objCnt; q++)
            {
                MobPool.Add(CreateObj(Mobs[i], transform ));
            }
        }
    }

    private void Start()
    {
        StartCoroutine(CreateMob());
    }

    IEnumerator CreateMob()
    {
        while (true)
        {
            MobPool[DeactiveMob()].SetActive(true);
            yield return new WaitForSeconds(Random.Range(1f, 3f));
        }
    }

    int DeactiveMob()
    {
        List<int> num = new List<int>();
        for (int i = 0; i < MobPool.Count; i++)
        {
            if (!MobPool[i].activeSelf)
                num.Add(i);
        }

        int x = 0;
        if (num.Count > 0)
            x = num[Random.Range(0, num.Count)];
        return x;
    }
    GameObject CreateObj(GameObject obj, Transform parent)
    {
        GameObject copy = Instantiate(obj);
        copy.transform.SetParent(parent);
        copy.SetActive(false);
        return copy;
    }
}

0개의 댓글