Menu
CoddyTech

A Falling Star

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 37 of 67.

challenge icon

Challenge

Medium

Make the star fall. Add two fields to Catcher:

  • [SerializeField] private Transform star; The scene already sets it to the Star.
  • [SerializeField] private float fallSpeed, which the scene sets to 3.

In Update(), move the star straight down at fallSpeed units per second. For now it falls forever; the next steps catch it.

Keep the catcher's movement and clamp from the last step.

Try it yourself

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

All lessons in Unity Scripting Basics