Menu
CoddyTech

넓은 패들

Coddy의 Unity 게임: 벽돌 깨기 코스 레슨. 19개 중 16번째.

시간제 효과는 코루틴입니다. 값을 바꾸고, 기다린 다음, 되돌립니다. 첫 번째 효과가 진행 중일 때 두 번째 효과를 얻으면 시간을 다시 시작해야 하므로, StartCoroutine이 반환하는 Coroutine을 저장하고 새 코루틴을 시작하기 전에 중지하세요. 그렇지 않으면 첫 번째 효과가 일찍 끝나 두 번째 효과를 되돌립니다. 속도 증가 효과는 다음과 같습니다.

if (boost != null) StopCoroutine(boost);
boost = StartCoroutine(BoostFor(3f));

IEnumerator BoostFor(float seconds)
{
    speed = normalSpeed * 2f;
    yield return new WaitForSeconds(seconds);
    speed = normalSpeed;
    boost = null;
}

현재 속도를 두 배로 만드는 대신 한 번 저장해 둔 normalSpeed를 기준으로 속도를 설정합니다. 효과가 연속으로 두 번 적용되면 속도가 4배가 되기 때문입니다. 패들의 제한 처리는 매 단계마다 패들의 너비를 확인하므로, 패들이 넓어도 벽에서 멈춥니다.

challenge icon

챌린지

어려움

Wide 파워업은 8초 동안 패들의 너비를 늘립니다.

  1. Paddle에 public float wideFactor = 1.6f;와 public float wideSeconds = 8f;를 추가하고, Awake에서 기본 x축 크기를 저장합니다.
  2. Widen()은 실행 중인 코루틴이 있으면 중지한 다음 코루틴을 시작합니다. x축 크기를 기본 크기에 wideFactor를 곱한 값으로 설정하고, wideSeconds만큼 기다린 뒤 기본 크기로 되돌립니다.
  3. 코루틴을 사용하려면 using System.Collections;가 필요합니다.

‘플레이’를 눌러 초록색 파워업을 받아 보세요. 패들이 커졌다가 다시 작아집니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

using UnityEngine;
using UnityEngine.InputSystem;

public class Paddle : MonoBehaviour
{
    public float speed = 14f;
    public float wallX = 8f;   // 양쪽 벽의 안쪽 면은 -wallX와 wallX에 있다

    Rigidbody rb;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float input = 0f;
        if (Keyboard.current.leftArrowKey.isPressed || Keyboard.current.aKey.isPressed) input -= 1f;
        if (Keyboard.current.rightArrowKey.isPressed || Keyboard.current.dKey.isPressed) input += 1f;

        Vector3 target = rb.position + Vector3.right * input * speed * Time.fixedDeltaTime;
        float halfWidth = transform.localScale.x / 2f;
        target.x = Mathf.Clamp(target.x, -wallX + halfWidth, wallX - halfWidth);
        rb.MovePosition(target);
    }

    public void Widen()
    {
        Debug.Log("Wide paddle");
    }
}

Unity 게임: 벽돌 깨기의 모든 레슨

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