Best / Easiest way to have the background follow the movement of the player/camera? (beginner level)

What is the best/easiest way to have a background sprite move with the player/camera?

If I make it a child of the player, when I change local scale to +1 or -1 to flip the character, the background flips.

I can do a work-around with having the background also flip scale to +1 or -1 so when the player is facing left, the background is facing opposite (still to the right) and when the player is facing right the background faces in the same direction (to the right).

This seems like a poor work-around.

I also tried making the background part of the canvas, but couldn’t figure out how to have it show up behind the player and other objects in the level.

1 Like

Create a new script .
Your camera is already following the player?
Did you try just making your background a child of the camera?

Else, you can add a script to your backgroud. Create a reference to your player Transform (call it something like playerTransform )in that script.

During Update, do that:

this.transform.position = new Vector3(playerTransform.position.x, playerTransform.position.y, this.transform.position.z);
(the “this” shouldn’t be needed and you can get rid of it)

You can do the same with the camera transform instead. ( if it feels somehow buggy with the camera transform, try to change the position during void LateUpdate() instead of Update. )

2 Likes

I was wondering if there was a way to do it without scripting. Just a way to use the hierarchy or a checkbox in the Inspector window. The two ways I have done it so far are:

  1. Make the camera and background children of the player. But when the player’s scale changes from (1,1,1) to (-1,1,1) to face left, the background flips. So I attach the same scale change script to the camera so it double flips (as I mentioned in the first post)

  2. Not having the background as a child object, but instead just attaching a script and using scripting to set the position of the background to the position of the player or camera (camera is centered on the player at the moment). This is as you described.

I was just curious if there was a way to use the Canvas or Main Camera to have something stick with it and never flip/rotate.

You could try to modify the sorting layer of the background once it is in the canvas.