• 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
1
Question by Petkopetko · Nov 01, 2020 at 09:43 AM · transform.positioncamera movementstartupparent 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How do I move both the parent and child to a known position where I want the child to be? 1 Answer

setting camera to hit.point returning null vector 1 Answer

How do I move it along with the platform while still keeping it's original scale!! 0 Answers

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

Cannot position child object relative to parent. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges