Blocks from the Sky
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 76 of 85.
Challenge
MediumSpawn a block in the sky every second. Keep the steering.
- Add
[SerializeField] private GameObject blockPrefab;,[SerializeField] private float spawnEvery = 1f;andprivate List<GameObject> blocks = new List<GameObject>();. - Add
Time.deltaTimeto afloat timer; when it reachesspawnEvery, takespawnEveryoff it. - Then spawn a block at
new Vector3(Random.Range(-laneEdge, laneEdge), 10f, 0f)and add it toblocks.
Press Play: blocks appear high above the lane. Then Check.
Try it yourself
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;
}
}
All lessons in Unity Scripting Basics
1C# Basics in Unity
Hello, UnityVariablesDecimals Need an fText in the ConsoleVariables That RememberIf and ElseCompare NumbersRecap: Coin Collector2Make It Move
Place the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube5Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot8GameObjects and Components
References in the InspectorGet a ComponentMissing ComponentsAdd a ComponentRequireComponentShow and HideRecap: Light Switch6Transforms and Hierarchy
Parents and ChildrenHoist the FlagScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon9Project: Catch the Star
The CatcherA Falling StarPractice on your own: Unity playground