Menu
CoddyTech

Cada vez más rápido

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

challenge icon

Desafío

Intermedio

Haz que cada captura acelere las estrellas.

  1. Añade [SerializeField] private float speedUp = 0.5f;.
  2. En el if de captura, junto a score++;, añade fallSpeed += speedUp;. No lo pongas en ResetStar(): también se llama cuando no atrapas una estrella.

Pulsa «Jugar» y atrapa unas cuantas: cada estrella cae más rápido. Después, pulsa «Comprobar».

Pruébalo tú mismo

using UnityEngine;

public class Catcher : MonoBehaviour
{
    [SerializeField] private float speed = 6f;
    [SerializeField] private Transform star;
    [SerializeField] private float fallSpeed = 3f;
    private int score;

    void Update()
    {
        float x = transform.position.x;
        if (Input.GetKey(KeyCode.RightArrow))
        {
            x += speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            x -= speed * Time.deltaTime;
        }
        x = Mathf.Clamp(x, -4f, 4f);
        transform.position = new Vector3(x, transform.position.y, transform.position.z);

        star.position += Vector3.down * fallSpeed * Time.deltaTime;

        if (Vector3.Distance(transform.position, star.position) < 1f)
        {
            score++;
            Debug.Log("Caught! Score: " + score);
            ResetStar();
        }
        else if (star.position.y < 0f)
        {
            ResetStar();
        }
    }

    void ResetStar()
    {
        star.position = new Vector3(Random.Range(-4f, 4f), 6f, 0f);
    }
}

Todas las lecciones de Fundamentos de scripting en Unity

Practica por tu cuenta: Playground de Unity