Make objects pushable
Tutorial
·
Beginner
·
+10XP
·
15 mins
·
(44)
Unity Technologies

The distinctive move in a Sokoban puzzle is the player pushing obstacles around the grid. In this tutorial, you’ll learn the calculations required to make one moving object appear to push another across Clive’s Crypt, so that you can script this functionality and customize it as you continue to develop this level.
By the end of this tutorial, you’ll be able to:
- Write a visual script that calculates and adjusts a GameObject’s position.
- Include a visual script inside another.
- Given a desired result, code an appropriate visual script.
Languages available:
1. Overview
Clive the Cat’s Visual Crypting is based on the game Sokoban, which originated in Japan. The word “Sokoban” means “warehousekeeper.” In the original game, the player character pushes crates around a warehouse to position them on certain goal cells.
In this level of the game, there are two pushable objects: Statue and Crate. These are not yet scripted. Writing that script is your task in this tutorial. You’ll make the Statue GameObject pushable, then you’ll be able to apply the same script to the Crate GameObject.
2. How pushing works
If you are interested in the calculations that make the Statue GameObject move in the correct direction, read on! If not, feel free to skip this step.
To push the Statue GameObject, located in the third room, Clive attempts to enter the cell where it is located. If you try this now, Clive will jump into and through the statue like a ghost. Instead, you want the statue to move in the direction Clive pushes it.
Several custom nodes, programmed in C#, are available to help you calculate where the statue should move. These nodes use Vector2 structures, which consist of two floating-point numbers: the X and Z coordinates of the cell grid.
The logic of the script you need for the Statue GameObject is as follows:
When Clive wants to enter the cell where the Statue is, the script does the following:
- Get the location of the cell with Statue as a Vector2 (call this A).
- Get the location of the cell with Clive as a Vector2 (call this B).
- Subtract the two Vector2's to get A - B, which yields a Vector2 indicating the distance and direction the Statue should move from its current position.
- Add the calculated Vector2 to the Statue’s current position to get the new position.
- Try to move the Statue to the new position, if possible (using a C# method). If there is something else in the way, like a wall, it will not move.
- Check to see if the Statue’s last movement is valid (using a C# method which returns a Boolean indicating if the movement worked).:
- If the movement is valid and the Statue has moved, do nothing and let Clive’s movement proceed.
- If it is not valid, cancel Clive’s movement.
Here’s an example of the Vector2 calculations: if the statue is at (1,1) and Clive is at (2,1), then A - B = (-1,0), indicating that the statue should move one cell to the left. To get the coordinates of the new cell for the statue, add (1,1) and (-1,0) to get (0,1).

3. Script the pushable statue
Let’s create the script to make an object pushable and name it “PushableScript”. This script will run on the pushable GameObject (Statue or Crate) when Clive tries to enter the cell in which that object is in — which is how Clive pushes another Gameobject.
Here are descriptions of three C# nodes you will need to complete this task:
- Moving Object: Get Current Cell inputs a GameObject and outputs its cell location on the grid as a Vector2. Use this node to get the positions of both Clive and the Statue GameObject.
- Moving Object: Move inputs a GameObject and a Vector2 containing the target position and moves the GameObject to that position. Use this node to move the Statue GameObject.
- Moving Object: Movement Still Valid inputs a GameObject and outputs a Boolean that indicates if the last movement on the GameObject is a valid one (with no obstacles). Use this to test whether the Move node for the pushable object was successful. If not, cancel Clive’s movement.
With this information, try to build the visual script for the Statue GameObject on your own. Check your work using the image in the next step as needed. Be sure to save your work with Ctrl+S (macOS: Cmd+S).
Tip: If you’re following the math in this script, what can you do with groups and the comments in them to make it easy to remember how this script works?
Enter Play mode and see if Clive can push the Statue around the room!
Tip: If it is getting tedious to test the level, you can turn off visual scripts by disabling their Script Machine components. Clive will temporarily jump through closed doors, and you won’t have to get the key or push the button every time you test.
When you are successful, mark the next step complete. If you get stuck, you can follow the instructions and the screen image in the next step.
4. Script the pushable statue (instructions)
Here are step-by-step instructions for scripting a pushable object:
1. On the ObjectRoot > Statue Gameobject, add a Script Machine component (select Add Component in the Inspector and select Script Machine from the Add Component menu).
2. Create a new visual script on the statue’s Script Machine, named “PushableScript”.
3. Begin the flow with the Want To Enter Cell event node, which outputs the moving object (Clive) that wants to enter.
4. Add the Moving Object: Get Current Cell node to get the position of the statue’s current cell. Select This as the input to get the Statue GameObject.
5. Add another Moving Object: Get Current Cell node to get the position of Clive’s current cell. Connect the input from this node to the output of the Want To Enter Cell event node to get Clive’s GameObject.
6. Add a Subtract node to calculate the statue’s position minus Clive’s position. (You can use either Subtract (Math/Generic) or Subtract: Vector 2.)
7. Add the result of the calculation to the statue’s current position using an Add node. Note that the Add node allows you to specify the number of input values to add, which in this case is 2.
8. Add a Moving Object: Move node to attempt to move the statue. The inputs are This (the pushable object) and the output of the Add node. Connect the flow from the Want To Enter Cell event node to the input of this node.
9. Add a Moving Object: Movement Still Valid node to find out if the movement is valid. Connect the flow from the Moving Object: Move node to the input of this node.
10. Add an If node to branch the flow. If the statue can move, do nothing and Clive will move normally. If the statue can’t move, cancel Clive’s movement using a Cancel Movement node. Since the movement you are canceling is Clive’s, connect the Moving Object output from the Want To Enter Cell event node to the Cancel Movement node.
11. Press Ctrl+S (macOS: Cmd+S) to save your work.
Your graph should look like the image below.

To view or use this script, check out Final_PushableScript in the Samples subfolder.
5. Make anything pushable
The crate in the fifth room will also be a pushable object. If you like, you could even make the cardboard box where Clive starts this level and his food dish pushable objects. Because the script you’ve created for the pushable statue doesn’t use any variables, events, or other specific inputs, it can easily be a subgraph for any other GameObject.
To make the crate GameObject pushable, add a Script Machine to it and create a new visual script named "CrateScript". Use the Fuzzy Finder to find the PushableScript you just created and add it to this script. That is all you have to do!

This technique makes it possible to add other flows to the CrateScript visual script and include PushableScript as an additional flow.
6. What’s next?
By now you are getting comfortable, we hope, with turning a logical plan into a visual script. In the next tutorial, you’ll work more independently on some more features to give Clive the Cat an entertaining adventure.