Creating a shaky bridge
In this post, we will create a shaky bridge, a bridge that collapse as the player walks on it. For this we will create a sprite and add a Rigidbody2D component to it. We will then either activate or deactivate the gravity on this object so that it starts to fall only the player collides with it.
Adding your Character and a basic platform
So let’s get started:
- Launch Unity and create a new project.
- Please create a new scene
- Please select: Assets | Import Package | 2D from the top menu.
- A new window called Import Unity Package will appear.
- Please click on the button called Import to import all 2D assets.
- This will create a new folder called Standard Assets in the Project window, and within this folder, another folder called 2D which includes several 2D assets that we will use.
Once this is done, it is time to add our 2D character:
- From Unity’s folder Standard Assets | 2D | Prefabs drag and drop the prefab called CharacterRobotBoy to the Scene view.
As you will see, this will create a new object called CharacterRobotBoy in the Hierarchy view. It will also add a character to the scene, as illustrated on the next figure.
- You can set the position of this character to (0, 2, 0) using the Inspector.
- If you click on this character in the Scene view, and then look at the Inspector, you should see that it includes several components, including a Sprite Renderer (to display the character), an Animator component (for the walking or jumping animations), two colliders (circle and box colliders), a Rigidbody component (so that it is subject to forces, including gravity) along with two scripts used to control the character. We don’t need to know the content of the scripts for now; however, it is always good to have an idea of the different necessary component for this character.
Once this is done, you can play the scene and you will see that the character will fall indefinitely; this is because of its Rigidbody component which exerts gravity on this character and also due to the fact that there is no ground or platform under the character; so the next thing we will do is to create a simple platform.
- Please create a new box: from the Project window, select Create | Sprite | Square.
- This will create a Sprite asset;
- Please rename it platform.
Note that because this is an asset, it will be accessible throughout the project, irrespective of the scene that is opened.
- Once this is done, you can drag and drop this asset to the Scene view.
- This will create a new object called platform.
- You can then resize this object so that it looks like a platform and place it just below the character; for example, you could set its scale property to (18, 1, 1) and its position to (-6, -1.5, 0).
Next, we need to add a collider to this object so that the player effectively collides with it (and stops falling).
- Please select the object called platform (that you have just created).
- From the top menu, select Component | Physics | BoxCollider2D.
Last but not least, we will make sure that the main camera follows the character:
Following the player with cameras
Perfect. So we have a character that can move around the scene and jump on platforms; the only thing is that, whenever this character is outside the screen, we can’t see it anymore; so we will need to make sure that it is onscreen all the time; and this can be achieved by setting the main camera to follow this character. Luckily, as part of the 2D assets, Unity provides a simple script that can be applied to any camera so that it follows a specific target. So we will use this script on the main camera so that it follows the character.
So let’s do the following:
- In the Hierarchy window, please select the object called MainCamera.
- Then after locating the folder Standard Assets | 2D | Scripts in the Project view, drag and drop the script called Camera2DFollow from this folder to the object called MainCamera.
- Once this is done, please select the object called MainCamera.
- As you do so, you will see that it includes a new component, which is our script, and that this component also includes an empty field called target; this field will be used to specify the target for this camera (in our case, this will be the object CharacterRobotBoy).
- Please drag and drop the object called CharacterRobotBoy from the Hierarchy window to this field.
- Once this is done, please test the scene, and check that the camera is now focusing on your character.
Creating the bridge
In this section, we will create a bridge.
- Duplicate the platform and rename the duplicate shaky_step.
- Resize this new platform (shaky step), so that its x-scale attribute is 3.5
- Create a prefab from this object (shaky_step) by dragging and dropping this object o the Project window; this will create a new prefab called shaky_step
- Drag and drop this new prefab (shaky_step) to the Scene view three times to create three steps as per the next figure.
Once this is done, we will modify these objects:
- Please select thee three steps in the Hierarchy.
- From the top menu, select Component | Physics2D | RigidBody2D; this will add a Rigidbody component to these objects so that they can fall (i.e., subject to gravity).
Once this is done, we just need to make sure that these start to fall only when the player collides with them.
- Please create a new C# script called ShakyStep.
- Add the following code to it (new code in bold).
void Start () { GetComponent<Rigidbody2D> ().isKinematic = true; } void Update () { } void OnCollisionEnter2D (Collision2D coll) { GetComponent<Rigidbody2D> ().isKinematic = false; Destroy (gameObject, 3.0f); }
In the previous code:
- In the Start function, we access the Rigidbody2D component of the object linked to the script (this will be the shaky step), and we set the variable isKinematic to true; this will have the effects of keeping the object in place (removing gravity for the time-being).
- Then, whenever a collision is detected (we assume that only the player will collide with these steps, but we could also have checked for the name of the object colliding), we set the variable isKinematic to false, so that the step can start to fall; we also destroy the step after 3 seconds.
We can now save our script and prefab:
- Please save this script and drag and drop it on all the three steps.
- You can also update the corresponding prefab by selecting one of these steps and clicking the button called Apply located in the top right corner of the Inspector.
- After this, you can test the scene and check that the steps fall when the player jumps on them.
Once we know that this is working, we could also create a prefab called shaky_bridge that includes a series of 10 shaky steps; again, this will make future levels creation much easier.
- Please duplicate the shaky steps seven times and arrange them so that the10 steps form a bridge, with some tiny gaps between the steps.
- Create an empty object called shaky_bridge.
- In the Hierarchy, drag and drop all the shaky steps to this empty object so that they become children of this object.
- You can then drag and drop the object shaky_bridge to the Project window to create a corresponding prefab.
Last but not least, we will complete this level:
- Please duplicate the long platform and place the duplicate just after the last step (to the right of the scene).
This will be useful so that you can reuse this level later on if you wish, as part of your game, or simply re-use the prefabs shaky_step and shaky_bridge to add more challenge to your game.
** This post is an excerpt from the new book
“A Beginner’s Guide to 2D Platform Games ” ***