Menu
CoddyTech

Kazandın

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

challenge icon

Görev

Orta

Oyuncunun target puana ulaşınca kazanmasını sağla.

  1. Ball.cs dosyasına public int target = 10; ve bool IsPlaying() ekle; bu işlev timeLeft > 0f && score < target döndürsün.
  2. Update() ve FixedUpdate() içinde timeLeft > 0f yerine IsPlaying() kullan ve AddScore işlevine if (!IsPlaying()) return; ile başla.
  3. Puan satırından sonra, score >= target olduğunda You win! günlüğe yazdır.

“Oyna” düğmesine bas ve 8 puan topla. Ardından “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;
    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);
    }
}

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

Kendi başına pratik yap: Unity playground