Menu
CoddyTech

Kaybettin

Coddy'nin Unity Journey'sinin Fizik ve Oynanış bölümünün bir parçası. Ders 34 / 71.

Bu adım, Coin Rush oyununu tamamlıyor: kuvvetlerle itilen bir top, tetikleyici olan madeni paralar, tek bir betik tarafından tutulan bir skor ve oyunu bitiren bir saat.

challenge icon

Görev

Orta

Süre önce dolduğunda oyuncuya haber ver.

  1. Ball.cs dosyasındaki Update() içinde, Time's up! mesajını kaydeden satırı bul.
  2. Hemen ardından You lose! mesajını kaydet.

“Oyna” düğmesine bas ve saatin dolmasını bekle: Console'da önce Time's up! Score: 0, ardından You lose! görünür. Sonra “Kontrol et” düğmesine bas.

Kendin dene

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 iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Fizik ve Oynanış bölümündeki tüm dersler

Kendi başına pratik yap: Unity playground