Menu
CoddyTech

Du verlierst

Teil des Abschnitts Physik und Gameplay der Unity-Journey von Coddy. Lektion 34 von 71.

Dieser Schritt schließt Coin Rush ab: ein Ball, der durch Kräfte geschoben wird, Münzen, die Trigger sind, eine Punktzahl, die von einem Skript verwaltet wird, und eine Uhr, die das Spiel beendet.

challenge icon

Aufgabe

Mittel

Sag dem Spieler Bescheid, wenn zuerst die Zeit abläuft.

  1. Suche in Ball.cs in Update() die Zeile, die Time's up! ausgibt.
  2. Gib direkt danach You lose! aus.

Drücke „Spielen“ und lass die Zeit ablaufen: In der Console erscheint Time's up! Score: 0 und dann You lose!. Klicke anschließend auf „Prüfen“.

Probier es selbst

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 iconTeste dich selbst

Diese Lektion enthält ein kurzes Quiz. Starte die Lektion, um es zu beantworten und deinen Fortschritt zu speichern.

Alle Lektionen in Physik und Gameplay

Übe selbstständig: Unity-Playground