Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 siddharth3322 · Jan 17, 2014 at 01:28 PM · gameobjectunity4.3localposition

In sequence position game objects

For my 2d game I want to post 10 heart objects in sequence on after other. For this I write following code but it does not work for me. So I want some suggestion in this.

     private const float HEART_SIZE = 45f;
     public GameObject heartLightPrefab, heartDarkPrefab;
 
     void Start()
     {
         float startX = 0f,startY=0f;
         for (int i = 0; i < Constants.TOTAL_HEARTS; i++)
         {
             Vector3 heartPosition = new Vector3(startX,startY,0f);
             GameObject heartObject =  Instantiate(heartLightPrefab, heartPosition, Quaternion.identity) as GameObject;
             heartObject.transform.parent = gameObject.transform;
             heartPosition = Camera.main.ScreenToWorldPoint(heartPosition);
             heartObject.transform.localPosition = heartPosition;
             startX += HEART_SIZE;
         }
 
     }

Using above code objects start generation from left side of the camera and finishes before it get chance to view in camera.

Also I am trying to position object based on relative to other collection object which is empty game object.

EDIT : At present objects positioned

alt text

heartobjectposition.png (30.3 kB)
Comment
Add comment · Show 4
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
avatar image MrVerdoux · Jan 17, 2014 at 01:45 PM 1
Share

Have you debugged the heartPositions once they are converted to world coordinates? depending on your resolution and the position of your camera they might not be what you expect.

avatar image siddharth3322 · Jan 17, 2014 at 01:58 PM 0
Share

Yes, I debugged my objects positions. I have edited my question.

avatar image MrVerdoux · Jan 17, 2014 at 02:01 PM 0
Share

If you are going to set position later, why don´t you use the one-parameter version of instantiate? Also, screenToWorldPoint can be unpredictable sometimes, couldn´t you use regular world coordinates?

avatar image siddharth3322 · Jan 17, 2014 at 02:02 PM 0
Share

Thanks for your answer but you have to give me more information. I will change this thing.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MrVerdoux · Jan 18, 2014 at 06:36 PM

Place an object at the left side of the screen, with z = -20; another one at the right side also with z = -20. Check their x´s, the difference is the width of your screen in world coordinates at a that certain z. Divide it by 10, and that should be the value of HEART_SIZE.

But as I said in the comments, I would do that in world coordinates directly, which are much easier to handle. That should be something like this:

     private const float HEART_SIZE = 45f;
     public GameObject heartLightPrefab, heartDarkPrefab;
  
     void Start()
     {
         float startX = 0f,startY=0f;
         for (int i = -Constants.TOTAL_HEARTS/2; i < Constants.TOTAL_HEARTS/2; i++)
         {
             GameObject heartObject =  (GameObject) Instantiate(heartLightPrefab);
             heartObject.transform.parent = gameObject.transform;
             heartObject.transform.localPosition = new Vector3(i*HEART_SIZE,0,0);
         }

         gameObject.transform.position = whereverYouWant;
     }
Comment
Add comment · Show 1 · Share
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
avatar image siddharth3322 · Jan 20, 2014 at 06:51 AM 0
Share

I got many things from you answer so thanks for it as well I post my code that work for me.

  for (int i = 0; i < Constants.TOTAL_HEARTS; i++)
         {
             GameObject heartObj = Instantiate(heartLightPrefab) as GameObject;
             float objectSizeX =  heartObj.renderer.bounds.size.x;
             heartObj.transform.parent = gameObject.transform;
             heartObj.transform.localPosition =new Vector3(i * objectSizeX * 2f, 0, 0);
         }

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Object dragging at manual point 2 Answers

GameObjects creation within boundry 3 Answers

At same positioned game objects collision detection 0 Answers

Game Development Approach 0 Answers

Access Prefab Object Via Scirpt 2 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