Menu
CoddyTech

땅에 닿거나 빗나가거나

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

이 과제에서는 타워 게임과 과정의 첫 번째 섹션을 마무리합니다. 크레인, 떨어지는 블록, 그리고 Mathf.Abs를 사용한 착지 테스트를 다룹니다.

challenge icon

챌린지

어려움

각 블록이 쌓이거나 떨어지게 하세요.

  1. 떨어지는 블록이 top의 y + 1까지 내려오고 top의 x에서 1 이내에 있으면, y를 정확히 그 값으로 설정하고, 해당 블록을 top으로 만들고, falling을 비운 다음 "Stack: " + count를 기록하세요.
  2. 그렇지 않고 y = -3 아래로 내려가면, 블록을 파괴하고 falling을 비운 다음 Missed!를 기록하세요.

‘플레이’를 눌러 블록을 쌓으세요. 블록이 착지할 때마다 높이가 기록됩니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

using UnityEngine;

public class Crane : MonoBehaviour
{
    [SerializeField] private GameObject blockPrefab;
    [SerializeField] private Transform top;
    [SerializeField] private float craneSpeed = 3f;
    [SerializeField] private float craneEdge = 4f;
    [SerializeField] private float fallSpeed = 7f;

    private float direction = 1f;
    private Transform falling;
    private int stack;

    void Update()
    {
        transform.position += new Vector3(direction * craneSpeed * Time.deltaTime, 0f, 0f);
        if (transform.position.x > craneEdge) direction = -1f;
        if (transform.position.x < -craneEdge) direction = 1f;

        bool drop = Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0);
        if (drop && falling == null)
        {
            Vector3 spot = new Vector3(transform.position.x, transform.position.y - 1f, 0f);
            falling = Instantiate(blockPrefab, spot, Quaternion.identity).transform;
        }

        if (falling != null)
        {
            falling.position += Vector3.down * fallSpeed * Time.deltaTime;
            // 쌓인 블록 위에 착지하기: 다음 강의에서 다룹니다.
        }
    }
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

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

직접 연습해 보세요: Unity 플레이그라운드