Menu
CoddyTech

Una estrella fugaz

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

challenge icon

Desafío

Intermedio

Haz que la estrella caiga.

  1. Añade [SerializeField] private Transform star; y [SerializeField] private float fallSpeed = 3f; a Catcher. La escena coloca la estrella en star.
  2. Al final de Update(), añade Vector3.down * fallSpeed * Time.deltaTime a star.position, no a la posición del receptor.

Pulsa «Jugar»: la estrella cae más allá del receptor y sigue cayendo. Después, pulsa «Comprobar».

Pruébalo tú mismo

using UnityEngine;

public class Catcher : MonoBehaviour
{
    [SerializeField] private float speed = 6f;

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

Todas las lecciones de Fundamentos de scripting en Unity

Practica por tu cuenta: Playground de Unity