Menu
CoddyTech

Schlag die Uhr

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

challenge icon

Aufgabe

Mittel

Füge Coin Rush eine Uhr hinzu.

  1. Füge in Ball.cs public float timeLimit = 60f; und public float timeLeft; hinzu; setze timeLeft = timeLimit; in Start().
  2. Ziehe in einem neuen Update() Time.deltaTime von timeLeft ab, solange timeLeft > 0f; setze den Wert bei 0 auf 0f und gib "Time's up! Score: " + score aus.
  3. Wende in FixedUpdate() nur dann Kraft an, wenn timeLeft > 0f.

Drücke „Spielen“ und beobachte die Uhr. Klicke dann auf „Prüfen“.

Probier es selbst

using UnityEngine;

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

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

    void FixedUpdate()
    {
        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);
    }
}

Alle Lektionen in Physik und Gameplay

Übe selbstständig: Unity-Playground