Menu
CoddyTech

정리하기

Coddy Unity 여정의 Unity 스크립팅 기초 섹션에 포함된 레슨. 67개 중 62번째.

challenge icon

챌린지

중급

블록이 사라지지 않습니다. 1분 동안 플레이하면 60개의 블록이 바닥 아래로 떨어지지만, 스크립트는 여전히 모든 블록을 움직입니다. 이전 단계의 내용을 모두 유지하고, 블록을 움직이는 루프를 변경하세요:

  • y가 -1보다 작은 블록은 바닥 아래에 있습니다. 해당 블록을 파괴하고 blocks에서 제거하여 루프가 더 이상 움직이지 않도록 하세요.

Play를 누른 다음 Check를 누르세요.

직접 해보기

using System.Collections.Generic;
using UnityEngine;

public class Runner : MonoBehaviour
{
    [SerializeField] private float speed = 6f;
    [SerializeField] private float laneEdge = 4f;
    [SerializeField] private GameObject blockPrefab;
    [SerializeField] private float spawnEvery = 1f;
    [SerializeField] private float fallSpeed = 5f;

    private List<GameObject> blocks = new List<GameObject>();
    private float timer;

    void Update()
    {
        Vector3 p = transform.position;
        p.x += Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
        p.x = Mathf.Clamp(p.x, -laneEdge, laneEdge);
        transform.position = p;

        timer += Time.deltaTime;
        if (timer >= spawnEvery)
        {
            timer = 0f;
            Vector3 spot = new Vector3(Random.Range(-laneEdge, laneEdge), 10f, 0f);
            blocks.Add(Instantiate(blockPrefab, spot, Quaternion.identity));
        }

        foreach (GameObject block in blocks)
        {
            block.transform.position += Vector3.down * fallSpeed * Time.deltaTime;
        }
    }
}

Unity 스크립팅 기초의 모든 레슨