Menu
CoddyTech

Bloques desde el cielo

Parte de la sección Fundamentos de scripting en Unity del Journey de Unity de Coddy. Lección 76 de 85.

challenge icon

Desafío

Intermedio

Genera un bloque en el cielo cada segundo. Sigue dirigiendo.

  1. Añade [SerializeField] private GameObject blockPrefab;, [SerializeField] private float spawnEvery = 1f; y private List<GameObject> blocks = new List<GameObject>();.
  2. Añade Time.deltaTime a un float timer; cuando alcance spawnEvery, réstale spawnEvery.
  3. Después, genera un bloque en new Vector3(Random.Range(-laneEdge, laneEdge), 10f, 0f) y añádelo a blocks.

Pulsa «Jugar»: los bloques aparecen muy por encima del carril. Después, «Comprobar».

Pruébalo tú mismo

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;
    }
}

Todas las lecciones de Fundamentos de scripting en Unity

Practica por tu cuenta: Playground de Unity