Menu
CoddyTech

패배!

Coddy Unity 여정의 물리와 게임플레이 섹션에 포함된 레슨. 71개 중 34번째.

이 단계에서는 Coin Rush를 완성합니다. 힘으로 밀리는 공, 트리거 역할을 하는 코인, 하나의 스크립트가 관리하는 점수, 그리고 게임을 끝내는 시계를 구현합니다.

challenge icon

챌린지

중급

시간이 먼저 다 되면 플레이어에게 알려 주세요.

  1. Ball.cs의 Update()에서 Time's up!을 기록하는 줄을 찾으세요.
  2. 그 바로 다음에 You lose!를 기록하세요.

‘플레이’를 누르고 시간이 다 될 때까지 기다리세요. Console에 Time's up! Score: 0이 표시된 다음 You lose!가 표시됩니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

using UnityEngine;

public class Ball : MonoBehaviour
{
    public float force = 10f;
    public int score;
    public float timeLimit = 60f;
    public float timeLeft;
    public int target = 10;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        timeLeft = timeLimit;
    }

    void Update()
    {
        if (IsPlaying())
        {
            timeLeft -= Time.deltaTime;
            if (timeLeft <= 0f)
            {
                timeLeft = 0f;
                Debug.Log("Time's up! Score: " + score);
            }
        }
    }

    void FixedUpdate()
    {
        if (IsPlaying())
        {
            float x = Input.GetAxis("Horizontal");
            float z = Input.GetAxis("Vertical");
            rb.AddForce(new Vector3(x, 0f, z) * force);
        }
    }

    public void AddScore(int points)
    {
        if (!IsPlaying())
        {
            return;
        }
        score += points;
        Debug.Log("Score: " + score);
        if (score >= target)
        {
            Debug.Log("You win!");
        }
    }

    bool IsPlaying()
    {
        return timeLeft > 0f && score < target;
    }
}
quiz icon실력 점검

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

물리와 게임플레이의 모든 레슨

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