making a quad follows the camera (child-parent doesn't work)

I’m making a 2D infinite runner game, so now I wanted to add a background for the game, so I made a quad and attach the image as texture with offset looping so it will show the repeating effect.

here’s the script of the looping background which works great

 using UnityEngine;
public class Background : MonoBehaviour {
     public float speed = 0.5f;
     void Update() {
         Vector2 offset = new Vector2(Time.time * speed, 0);
         GetComponent<Renderer>().material.mainTextureOffset = offset;
     }
}

Now the problem, my camera is an orthographic and made follows the Player buy through code so the camera view won’t jump when the Player jump, but I can’t make the background (AKA quad) follows the camera…!?? because the camera moves to positive x endlessly but the background is still…!

the most obvious way is by make the Background a child to the camera, but once I do that the background transform value reset as the Camera and it stopped showing…!?

The camera z-position is -20 with (0,0,0) scale and the background has z-position of 10 with (35,20,0) scale …!!

so How can I make the Background follow a moving camera endlessly without changing the original values of the z-position or the scale…!?

here’s a video that i recorded which concise the problem…

If the Camera script is wanted, I’ll add it…!

That video doesn’t show enough detail-- we can’t see what you’re doing to the transform values. There doesn’t appear to be a need to set the background’s parent in Update() by the way, doing it in Start() makes more sense since it only needs to be set once.

You should be able to set the quad as a child of the transform without any problems as long as the localPosition of the quad is set to the correct value (z-position should be +30), but if you already have a scrolling background, you could keep the camera and background quad still and make the platforms move to the left past the play area.

1 Like

the new values of transform are mentioned…!

Quad position = (0,0,10) & scale = (35,20,0)
Camera position = (0,0,-20) & scale = (0,0,0)

when I make the Quad as child for the camera ==> the quad transform values become like the camera’s values !!?

and I don’t want that…!! I want to maintain the quad original transform values yet still follows the camera…!

about that line in update, I forgot to erase it since it does nothing, I add it because I thought it could solve the issue but it didn’t.

Have you tried setting the position values after assigning the parent instead of before? Parenting will do exactly what you’re looking for, but I think objects can get set to local (0, 0, 0) after assignment. Not 100% sure of that though.

The problem is that the child position values inherits the patents which the camera and you can’t change the child values …only the parent, but I need the z-position of the child to be way in the positive (0,0,20) yet the camera still remains (0,0,-20) !

I’m sure it can be solved by code, what should I write in the Background script or the CameraController script which will allow to do what I wanted ??

This isn’t how parenting works. Children exist in a local space centered on the parent’s transform; e.g. if you move the parent, the child will move in the same directions and about the same axes, but you can still move the child independently. You shouldn’t have to do absolutely anything in scripting to achieve the behavior that you seem to be asking for.

If your really can’t change the child’s position values, then I admit I’m not exactly sure what’s going on. What happens when you try? Do the values get set back to 0 instantly? Are the values somehow deactivated (“greyed out”)?

Can we have a copy of your project to have a look? Intuition tells me that that there’s either a script somewhere that’s misbehaving or that we have a miscommunication occurring.

well, I not that expert in Unity… but that’s what happens to me… and I cant quite explain it since I don’t why it’s happening… if you open the video I attached and look carefully at 00:20 you will see when I make the background a child of the camera… it disappears and all of its Transform values become zeros…!!

anyhow… I found a solution in Answers… but through code… I made the background take the Player last x position and subtract it with the background x position then I add the difference to the background current x position…!

here’s it https://goo.gl/b7IFjC

Thanks anyway for your help :smile: