• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Petkopetko · Nov 01, 2020 at 09:43 AM · transform.positionstartupcamera movementparent transformparent and child

Position of cameras children objects messed up after the game start.

Hi! I am creating my first 2d game in unity and I have created a project with a camera and StatickBackground child object that supposed to move with camera. The camera is then following a player game object. At the beginning of the game, static background is only 1 star and others will be cloned in code after the game starts.

 public class CreateStaticBg : MonoBehaviour
 {
     public UnityEngine.GameObject stars;
     public static readonly Random rand = new Random(1234);
 
     void Start()
     {
         
         for (int i = rand.Next(500) + 10; i >= 0; i--)
             Instantiate(stars, new Vector3(rand.Next(-8000, 8000), rand.Next(-5000, 5000), transform.position.z), Quaternion.identity, transform);
     }
 }

And camera follow code that is attached to MainCamera object:

 public class CameraFollow : GameObject
 {
     public Transform trans;
 
     void Update()
     {
         transform.position = new Vector3(trans.position.x, trans.position.y, -20);
     }
 }

As you can see everything is aligned perfectly since the game is in editor mode and is not playing.
https://answers.unity.com/storage/temp/170233-in-editor.png
However, when I start the game, static background suddenly moves to the x 0 of the world and not stays on the parent ones.
https://answers.unity.com/storage/temp/170234-after-play.png
I have a theory that this is somehow caused by star generation however stars are generated in both left and right from point 0 of StaticBackground. I also tried to set the position of static background back to 0 but this also did not help. The only thing that help was to manually grab the static background in scene view and put it where it supposed to be... In this case its X will end on ~ -6000 which only gives to my theory that static background somehow detaches itself from the camera parent move to [0, 0] of the world and come back to camera.
Any ideas about why is this happening and how to fix it?
Please help.

after-play.png (177.7 kB)
in-editor.png (146.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help updating camera position to go above the player 2 Answers

Cannot position child object relative to parent. 3 Answers

Converting local position movement to global position movement? 1 Answer

Parenting an object changes its transform. 0 Answers

Keeping (potentially hundreds) of object's transforms relative to animated counterparts? (Without Parenting) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges