In this article, I will introduce how to create an scene where objects appear in the sky, drop down, and keep on piling up on each other.
I will mainly use Unity PlayMaker to implement this scene.
- 1 Sample
- 2 Preparation
- 3 Create a floor
- 4 Create the objects that you are going to drop
- 5 Creating a vendor machine
- 6 Dropping an object
- 7 Looping the drop action
- 8 Randomizing the spawn position
- 9 Prevent the vending machine from slipping through the floor
- 10 Set a parent to the vending machine
- 11 Upload to STYLY
Download a FBX file from Google Poly
Download a FBX file from Google Poly.In this tutorial, I downloaded a FBX file of a vendor machine.
Import the downloaded FBX file to Unity
Next, we need to import the downloaded FBX file to Unity.
Create a floor
We then need to create a floor, that the objects will drop on to.
Create the objects that you are going to drop
Next, we want to set it so that the vendor machines drop from the sky. In order to do so, we need to create a manager which will control the dropping motions.
So let’s create that manager.
Use Create Empty to create an object, and set the name to ‘vending create’.This vending create will act as the manager for the movements.
Creating a vendor machine
Create a vendor machine prefab
Let’s create a vendor machine. First, we need to change it into a prefab.
Drag and drop the vendor machine(model) into the Hierarchy, then return it to the Project tab again.
This time, change the vendor machine(model) name to ‘vending_machine’.
Do not delete the original imported vending machine here. It is needed.
The result should be as in the image below.
Create an event with PlayMaker
Select vending_create, then choose add state from the PlayMaker window.
Click add state, and the screen will become like this. Select the Events tab and create a ‘create vending machine’ event.
Add a Create Object action.
Then, add the vending machine prefab to the Create Object action.,
Drag and drop the vending_machine to the Game Object column.
We want the vending machine to spawn slightly above the ground, so set the Position to (0, 2, 0).
Click the Play button, and a floating vending machine will appear.
Dropping an object
We were able to create a vending machine in the air.
But it is currently just floating, so we need to change settings so it will drop to the ground.
Click vending_machine, and choose the Add Component in the Inspector tab, then add a Rigidbody component to the vending machine prefab.
Click the Play button, and the vending machine will drop from the sky.
Looping the drop action
All we need to do now is to loop the vending machine drop action.
Let’s set it so that the drop action will take place every 2 seconds.
In order to loop the create vending machine event, create an arrow starting from State 1 and returning to State 1, as in the image below.
Then, create a Wait action, and set the Time to 2. Set the Finish Event to ‘Create Vending Machine’.
This setting will loop the create vending machine event for every 2 seconds.
So, a vending machine will appear in the air and drop to the ground every 2 seconds now.
Randomizing the spawn position
Since the spawn position is the same, the vending machines will be created on top of each other, and will become some sort of tower. We need to use Random Float and Set Vector3 XYZ to randomize the spawn position.
Create Random Float and Set Vector3 XYZ, and set the following values to them.
Random Float will create a random value between -2 and 2, and store that value in the ‘random_pos’ variable.
Set Vector3 XYZ will set the X-axis, Y-axis, and Z-axis.
In the image below, random_pos is applied to X, 5 is applied to Y, and random_pos is applied to Z. The XYZ-axis will get stored in the variable pos.
With these settings, the vending machine spawn position is now randomized.
Prevent the vending machine from slipping through the floor
Currently, the vending machine will just slip through the floor, so we need to use Physic Material to prevent that.
First, we need to create a Physic Material. Name it ‘plane_physic_material’.
Set the values as in the image below.
Set it to the Material of the Plane Mesh Collider.
Then add a Box Collider component to the vending_machine, and add plane_physic_material to the Box Collider material.
With these settings, the vending machine will not slip through the ground anymore.
Set a parent to the vending machine
It isn’t neat for vending machines to be created right under Hierarchy, so use the Set Parent action to set a parent.
With this setting, the created vending machines will be created under vending_create, and will become organized.
Create a Set Parent action under the Create Object action.
Set the values as in the image below.
Add the object created by the Create Object action to the ‘created_obj’ variable.
We will use that variable inSet Parent.
Click the Play button, and you will see that the created vending machines will be created under vending_create.
Upload to STYLY
How to upload an asset from Unity to STYLY
In this article, we created a scene where objects are continuously created and piled up on each other.
You can change the object, or how many objects spawn at once, or change the loop time to create a scene that you want.
I hope you use this technique for your future creations.