Tower Builder
Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 84 of 85.
A crane slides back and forth above a base. Each press of Space, or each click, drops a block straight down from the crane. A block that comes down on top of the stack stays there, and the next block has to land on it. A block that misses falls away. The height of the stack is the score.
Write the whole game in one script, Crane, on the crane.
Challenge
HardStart the tower game: a crane that slides and drops blocks.
- Slide the crane along x at
craneSpeed, turning back pastcraneEdgeand-craneEdge. - Space or a left click drops a Block 1 below the crane into a
Transform fallingfield, only whilefalling == null. - Move the falling block down at
fallSpeed.
Press Play and press Space: a block falls from the crane. Then Check.
Try it yourself
using UnityEngine;
public class Crane : MonoBehaviour
{
[SerializeField] private GameObject blockPrefab;
[SerializeField] private Transform top;
[SerializeField] private float craneSpeed = 3f;
[SerializeField] private float craneEdge = 4f;
[SerializeField] private float fallSpeed = 7f;
void Update()
{
}
}
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