Tüm Rehberler

Top Zıplat — Rigidbody Fizik Oyunu

Başlangıç ⏱ 75 dk
BÖLÜM 5 / 8

Puan Toplama — Collectible Sistemı

Platformda dağılmış dönen yıldız/puan nesnelerine topu değdirerek puan toplama sistemi kuruyoruz.

Unity Editör Adımları

'+' > 3D Object > Sphere küçük → 'Coin', Scale:(0.4,0.4,0.4)
Coin > Sphere Collider → isTrigger = TRUE
Assets > C# Script → 'Collectible'
Coin objesini Prefab yapıp sahnede çoğalt
csharp
using UnityEngine;

public class Collectible : MonoBehaviour
{
    public int scoreValue = 10;
    public float rotateSpeed = 90f;
    public GameObject collectEffect;

    void Update()
    {
        // Dönen yıldız efekti
        transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
        // Yukarı aşağı yüzme
        transform.position += Vector3.up *
            Mathf.Sin(Time.time * 2f) * 0.003f;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            ScoreManager.Instance.AddScore(scoreValue);
            if (collectEffect)
                Instantiate(collectEffect, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
    }
}

// ScoreManager.cs
public class ScoreManager : MonoBehaviour
{
    public static ScoreManager Instance;
    public TMPro.TMP_Text scoreText;
    private int score;
    void Awake() { Instance = this; scoreText.text = "0"; }
    public void AddScore(int v)
    {
        score += v;
        scoreText.text = score.ToString();
    }
}

Mathf.Sin() ile yüzen hareket efekti çok az CPU kullanır ama oyunu canlı gösterir.

Önceki Bölüm Sonraki Bölüm