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
1
Question by Wuzseen · Jan 14, 2013 at 03:16 AM · instantiaterendererbounds

Instantiate Left-Edge of Prefab at Right-Most Edge of Another

So I'm working on some randomly generated content and I've broken up my platforms into "frames"

When I get to placing a new frame I want to position it so its left edge is next to the left most edge of the previous one.

I keep track of the current right most frame in the code and here's my code where I instantiate the next frame:

         rightMostFrame = (GameObject)Instantiate( frameToAdd, 
             rightMostFrame.transform.position + rightMostFrame.transform.right * rightMostFrameComponent.offset - rightMostFrame.transform.up * rightMostFrameComponent.transform.position.y, 
             transform.rotation );

Frame to add are a bunch of prefabs that consist of a couple models as children. One of these models is the actual platform I want to position along the previous.

The code above, specifically rightMostFrameComponent.offset, comes from another script where I store data for each frames. I figure this would be useful. At creation of these frames I make this offset property equal to the X size of the objects renderer

         offset = floor.renderer.bounds.size.x;

I'm having trouble mapping out exactly how I should do this. I need the center of the new frame to be offset from the right edge of the previous frame enough so that the left side of the new frame is at the same X and Z coordinate.

Any ideas?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sharp7j · Nov 14, 2015 at 07:23 PM

So I don't know if this will work, but this is how I plan to do it.

You could go through all the objects in the previous prefab, get their x position and their widths, then see which one has the highest width+position (might be width/2+position?). That should be the right most point, however I don't know how to do this with rotations included.

Then to the prefab you want to add, you would do the same thing but get the left and right most points. You would want the right AND left most point just to get the total width of it. Then you would just create the prefab at I think old right most point+(total width of new prefab)/2.

Something like that might work if rotations aren't happening.

I think there might be something you can do with checking the rendering of the sprite as well?

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

Getting Collider2D.bounds.extents to update immediately (2D) 0 Answers

Set bounds of collider added at runtime 1 Answer

Instantiate GameObjects inside a collider 1 Answer

Can't render GameObject 3 Answers

Array of prefabs with different sizes stick to each other 0 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