Menu
CoddyTech

Punkte zählen

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

challenge icon

Aufgabe

Mittel

Zähle die Münzen: Jede Münze kennt ihren Wert, und der Ball behält die Gesamtsumme im Blick.

  1. Füge in Ball.cs public int score; und public void AddScore(int points) hinzu. Diese Methode addiert points und protokolliert "Score: " + score.
  2. Füge in Coin.cs public int value = 1; hinzu. Die große Münze hat den Wert 3.
  3. Rufe beim Einsammeln other.GetComponent<Ball>().AddScore(value); auf, bevor du die Münze ausblendest.

Drücke „Spielen“ und sammle alle Münzen ein: In der Konsole wird Score: 7 erreicht. Drücke dann „Prüfen“.

Probier es selbst

using UnityEngine;

public class Ball : MonoBehaviour
{
    public float force = 10f;
    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);
    }
}

Alle Lektionen in Physik und Gameplay

Übe selbstständig: Unity-Playground