A Falling Star
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 37 of 67.
Challenge
MediumMake 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
1Your First Script
Hello, UnityPlace the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube4Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot7Project: Catch the Star
The CatcherA Falling Star5Transforms and Hierarchy
Parents and ChildrenScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon