Pattern: Environment Setup – One Image

This tutorial will explain how to set up your game’s environment in Unity if you have a single image representing the game environment or map. This single image will include all environment features including background and collision elements.


WARNING
Note that this is NOT the recommended way to create your environment, and should only be used if you already have created your environment in this way.


Required Tasks or Knowledge

Video Tutorial

Step 0 – If using Unity Playground, temporarily turn it off

Unity Playground simplifies a lot of things, Unfortunately it also hides some useful features, such as the collider editor button. Turn it off while working on this setup, but don’t forget to turn it back on when you are done!

You can turn Playground on and off using the “Playground” menu in the main menus at the top of the screen.

Step 1 – Create a Scene

Create a scene for your environment to be placed in. You may want to name the scene something like “Level 1” if your game will be organised into multiple levels. If your game will be all one big level, you could name it something like “InGame”.

Create a scene by choose File > New Scene, and save the scene in the Scenes folder inside your project’s Assets folder.

Step 2 – Drag your background image into the scene

Drag your background image into the scene to create a GameObject for the background. Rename it “Background” or something similar. Resize it and position it as you want – remember to use the game camera to help guide you.

Step 3 – Create an empty GameObject

Create an empty GameObject using GameObject > Create Empty. Rename it to something like “Background Collision 1”. This will be used to represent part of the collision for the background.

Step 4 – Position the empty GameObject where you want collision on your background

Step 5 – Add a collider to the empty GameObject

This can be whatever type of collider you need based on this background. Most likely types include the Box and Circle colliders. Click on your object and click “Add Component”, then search for the type of collider you would like to add.

Step 6 – Edit the collider to match the image

You can manually adjust the collider values on the component you created, or you can edit the collider using the “Edit Collider” button on the component. It looks a bit like the Steam logo.


REMINDER
Remember that you can’t edit colliders using the button while Unity Playground is on – turn it off using the Playground menu at the top of the screen. Just remember to turn it back on when you are done!


Step 7 – Repeat steps 3-5 until all colliding areas of the background are accounted for

Step 8 – Add a collider to the player, if not already present

Choose whichever type of collider best suits the player’s shape. I recommend not using Box colliders as the player tends to get stuck on things with them.

Step 9 – Test collision by moving the player around the environment

Ensure that the player can fit where they need to, and collides with everything they should.

Troubleshooting common issues:

  • My player won’t fit through an area they need to fit through!

    Try reducing the size of the player’s collider, and make sure your environment colliders are also the right size. It’s okay if the player’s collider is a little smaller than them – this is actually better than it being a little bigger!
  • My player isn’t colliding with the environment!

    Make sure both the player and the area you are trying to collide with have colliders – click on their GameObject’s and make sure the collider component is expanded. The collider is represented as a green line.

    If it looks right, check that both colliders are 2D colliders (2D and 3D don’t work together).

    Finally, check if both colliders have “Is Trigger” UNticked.
  • My player is not moving at all, or is teleporting way out of the environment!

    Check that you don’t have a really large collider somewhere, perhaps on the environment image itself. This will cause you player to be colliding the moment the game starts, which can cause the above issues.
  • My player starts spinning when they touch the environment!

    This is due to physics controlling your player’s rotation. Fix this by selecting your player, looking at the Rigidbody2D component, and choosing Constraints > Freeze Rotation.
  • My player is sometimes hidden behind the environment!

    Your player needs to have a higher Sorting Layer or Order in Layer than your environment. Click on the Environment image GameObject, go to the Sprite Renderer’s Additional Settings, and change Sorting Layer to “Background”. Click on the player GameObject, go to the Sprite Renderer’s Additional Settings, and change Sorting Layer to “Gameplay”.
  • My player can’t jump when they are touching the ground (Unity Playground)!

    Your ground needs to be tagged as Ground so the player knows it’s allowed to jump after touching it. Click on each of the background collider GameObjects and change their tag (at the top of the inspector) to Ground.

Step 10 – If you are using Unity Playground, turn it back on

Make sure to turn Unity Playground back on if you turned it off to edit colliders. You can turn Playground on and off using the “Playground” menu in the main menus at the top of the screen.

Further Reading:

  • Patterns:
    • Environment Setup – Foreground Objects
    • Environment Setup – Tilemap
    • Environment Setup – Scrolling Objects
    • Level Transitions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s