상세 컨텐츠

본문 제목

7. 끝없이 무한 달리기 만들기

유니티

by MJ_119 2023. 6. 7. 05:01

본문

1. 프리펩 만들었으니 Basic Tile 제거하기

2. 새로 C# 스크립트 GameController 만들어서 작성하기

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

public class GameController : MonoBehaviour
{
    [Tooltip("생성하고자 하는 타일 참조")]
    public Transform tile;
    
    [Tooltip("첫 번째 타일이 생성되는 곳")]
    public Vector3 startPoint = new Vector3(0,0,-5);

    [Tooltip("사전에 몇 개의 타일이 생성되어야 하는가")]
    [Range(1,15)]
    public int initSpawnNum = 10;

    ///다음 타일이 생성되는 곳
    private Vector3 nextTileLocation;

    /// 다음 타일의 회전은?
    private Quaternion nextTileRotation;

    // Start is called before the first frame update
    void Start()
    {
        // 시작 포인트 설정
        nextTileLocation = startPoint;
        nextTileRotation = Quaternion.identity;
        for (int i = 0; i < initSpawnNum; i++)
        {
            SpawnNextTile();
        }
    }

    /// 특정 위치에 타일을 생성하고 다음 위치를 설정한다.
    public void SpawnNextTile()
    {
        var newTile = Instantiate(tile, nextTileLocation, nextTileRotation);

        // 다음 아이템을 생성할 위치와 회전 값을 알아낸다.
        var nextTile = newTile.Find("Next Spawn Point");
        nextTileLocation = nextTile.position;
        nextTileRotation = nextTile.rotation;
    }
}

3. 새로운 GameObject를 생성하고 Game Controller로 바꾼뒤 Hierarchy 창 제일 위쪽으로 올려놓는다.

4. Game Controller 를 누른뒤에 C# 스크립트를 추가해주고 프리펩 폴더에 있는 Basic Tile을 드래그 해서 인스펙터 창에 있는 Tile에 추가해준다. 

 

 

'유니티' 카테고리의 다른 글

9. 맵 장애물 만들기(재시작)  (1) 2023.06.07
8. 타일 박스 콜라이더에 닿으면 삭제하기  (0) 2023.06.07
6. 프리팹(Prefabs)만들기  (0) 2023.06.06
5. 맵의 끝 확인하기  (0) 2023.06.06
4. 벽 만들기  (0) 2023.06.06

관련글 더보기