3-2) Spawning Platforms Automatically

시그니천·2024년 6월 5일

[ G01 ] ZigZagRacer

목록 보기
10/26
post-thumbnail

업로드중..

1.PlayformSpawner

public class PlatformSpawner : MonoBehaviour
{
    public GameObject platform;

    public Transform lastPlatform;
    private Vector3 lastPosition;
    private Vector3 newPos;
    private float platformSize = 2f;

    private bool stop;

    private void Start()
    {
        lastPosition = lastPlatform.position;

        StartCoroutine(SpawnPlatforms());
    }

    private void Update()
    {

    }

    IEnumerator SpawnPlatforms()
    {
        while (!stop) 
        {
            GeneratePoition();

            Instantiate(platform, newPos, Quaternion.identity);

            lastPosition = newPos;

            yield return new WaitForSeconds(0.1f);
        }
    }

    private void GeneratePoition()
    {
        newPos = lastPosition;

        // Random으로 생성
        int rand = Random.Range(0, 2);

        if(rand > 0) 
        {
            newPos.x += platformSize;
        }
        else
        {
            newPos.z += platformSize;
        }
    }
}
profile
우주최강개발자

0개의 댓글