Menu
CoddyTech

Blöcke vom Himmel

Teil des Abschnitts Unity-Scripting: Grundlagen der Unity-Journey von Coddy. Lektion 76 von 85.

challenge icon

Aufgabe

Mittel

Erzeuge jede Sekunde einen Block am Himmel. Behalte die Steuerung bei.

  1. Füge [SerializeField] private GameObject blockPrefab;, [SerializeField] private float spawnEvery = 1f; und private List<GameObject> blocks = new List<GameObject>(); hinzu.
  2. Addiere Time.deltaTime zu einem float timer; sobald er spawnEvery erreicht, ziehe spawnEvery davon ab.
  3. Erzeuge dann einen Block bei new Vector3(Random.Range(-laneEdge, laneEdge), 10f, 0f) und füge ihn zu blocks hinzu.

Drücke „Spielen“: Blöcke erscheinen hoch über der Fahrbahn. Drücke dann „Prüfen“.

Probier es selbst

using UnityEngine;

public class Runner : MonoBehaviour
{
    [SerializeField] private float speed = 6f;
    [SerializeField] private float laneEdge = 4f;

    void Update()
    {
        Vector3 p = transform.position;
        p.x += Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
        p.x = Mathf.Clamp(p.x, -laneEdge, laneEdge);
        transform.position = p;
    }
}

Alle Lektionen in Unity-Scripting: Grundlagen

Übe selbstständig: Unity-Playground