Scripting a background image to follow the camera

Knowledgebase / Scripting a background image to follow the camera

If you have a large image that you want to use as a background image in your game, you can script it to make it follow the camera. In the level editor, right-click on the background game object, and select Add script.

pasted image 0 (9)

We want to make the background constantly follow the camera, so first we’ll need the “Constantly” block (from Events). We also want to set the x and y position position of the background, so grab the “set x position of myself” block (from Transform). Right-click on that block, and select duplicate to make another copy of it, then change x position to y position.

code-red-screenshots-2016-14

We want to set the position of this background to the position of the camera, so grab the “camera x” block from Looks, duplicate it, and make it “set x position of myself to camera x” and “set y position of myself to camera y”.

code-red-screenshots-2016-15

Since we don’t want our background colliding with things, we are going to disable physics on it as soon as it’s created.

Grab the “When created” block, then grab the “Set Physics enabled” block from Physics. Connect them together, and change true to false – this will disable physics on the background image.

code-red-screenshots-2016-16

Save this new script, name it Background, then play your game to see it working.

Note: Make sure your background object is on a layer that is underneath everything else, or it will cover things and hide them from view.