Menu
CoddyTech

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 icon

Challenge

Hard

Start the tower game: a crane that slides and drops blocks.

  1. Slide the crane along x at craneSpeed, turning back past craneEdge and -craneEdge.
  2. Space or a left click drops a Block 1 below the crane into a Transform falling field, only while falling == null.
  3. 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

Practice on your own: Unity playground