Menu
CoddyTech

승리!

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

challenge icon

챌린지

중급

플레이어가 target점에 도달하면 이기도록 하세요.

  1. Ball.cs에 public int target = 10;과 bool IsPlaying()을 추가하고, timeLeft > 0f && score < target을 반환하도록 하세요.
  2. Update()와 FixedUpdate()에서 timeLeft > 0f 대신 IsPlaying()을 사용하고, AddScore를 if (!IsPlaying()) return;로 시작하세요.
  3. 점수 줄 다음에 score >= target이면 You win!을 로그에 출력하세요.

‘플레이’를 누르고 8점을 모으세요. 그런 다음 ‘확인’을 누르세요.

직접 해보기

using UnityEngine;

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

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

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

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

    public void AddScore(int points)
    {
        score += points;
        Debug.Log("Score: " + score);
    }
}

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

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