- Home /
How to have child object only follow parent x position
I have a background image that was a child to the camera so that it moves with the camera, but when the camera rotates, i don't want the background to rotate with it.
I thought the obvious solution was to just set the bg to the camera's x position in the update function, but for some reason, this resulted in a very choppy background movement. I also tried forcing the bg's rotation to stay at 0, but that doesn't affect it's world rotation so it still rotates with the parent. I tried making the bg turn in the opposite direction from the camera, but this was difficult to make look good.
Is there a way to to get bg to follow the camera's x-position smoothly?
Hey @OmegaFalcon, have you tried using bg.transform.localEulerAngles(0f, 0f, 0f); ?
yes i have. Doing that doesn't make a difference since it only affects the local rotation. When the parent rotates, the child's rotates with it but it's local rotation is still 0
Answer by SquashedGames-Studios · Oct 22, 2020 at 01:04 AM
I think setting the bg to the camera's x position will work if you the bg is not a child of camera, does it have to be a child?
Doesn't have to be a child. When I was making it follow the camera's x transform, it wasn't a child of it then though so that doesn't work.
Answer by NullOverrideX · Oct 22, 2020 at 09:51 AM
I'm not sure, but you can try
transform.position = targetOb.position(x)
I don't have Unity near me and I'm only 9 years old so I'm not sure if this works. (My guess is probably not but this is the best I can do.
Answer by corapeca · Oct 23, 2020 at 03:10 AM
try freezing y and z movement,
make bg child of the camera, and add a rigidbody body to bg, then disable use gravity, then go to constraints, freeze all rotations and freeze movement of y and z.
I hope that might help.
Your answer
Follow this Question
Related Questions
How Do I Give Two Objects the Same Transform? 1 Answer
How to transform children of an object? 1 Answer
Unit Formation Aligns to a Reset Forward Position Rather Than Parent Object's Rotation 1 Answer
Make an object do exactly what another, but later 0 Answers
Set Position and Rotation with another GameObject as Pivot (Script) 0 Answers