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 ivarhill · Dec 26, 2012 at 04:03 PM · gameobjectinstantiatepositionrandompivot

Instantiate a GameObject at the position of one of its child objects

I'm working on a script to randomly generate a dungeon by connecting random rooms with each other based on their door positions. See picture:

alt text

To the left is an example of a room GameObject - containing a lot of child GameObjects being all the contents of the room. This includes its doors, depicted above. A room can have 1-4 doors, and I wish to instantiate new rooms at the positions of these doors (see picture in the middle).

My issue is that when I instantiate a new room GameObject, its pivot point is located in the center of the room - and thus, I don't know how to align it with the doors. I can set its height correctly by instantiating it at the location of the original room, then moving it up as much as the room is long. However, I cannot get the horizontal position to work since I don't know how to match up the door position (see picture to the right).

If I could tell it to be instantiated using the door location as its pivot, everything would work fine, but I don't know if this is possible or if it's the best solution. Any suggestions?

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
2
Best Answer

Answer by Fattie · Dec 26, 2012 at 05:15 PM

Sure, just use four markers

Funnily enough someone just asked about this:

http://answers.unity3d.com/questions/369013/is-there-any-way-to-track-the-position-of-a-certai.html

So your Room thing will have four markers, that is, empty game objects, and we'll call them doorMarkerA, doorMarkerB, doorMarkerC, doorMarkerD.

So you're about to position a new room. we'll call it NEWROOM.

Let's say you want the CENTER of NEWROOM to be at point PLACEITHERE

of course, you just do this

 NEWROOM .. get it from your pool
 NEWROOM.transform.position = PLACEITHERE

So that's obvious. Now, if you want DOOR "A" of the NEWROOM to be at the point PLACEITHERE,

Here is what you do:

 NEWROOM .. get it from your pool
 NEWROOM.transform.position = PLACEITHERE
 NEWROOM.transform.position -= doorMarkerA.localPosition

So that's exactly how you do it. You SUBTRACT the LOCAL POSITION of the relevant marker.

Hope it helps!

Comment
Add comment · Show 4 · 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 ivarhill · Dec 26, 2012 at 06:10 PM 0
Share

I'm afraid I cannot get it to work. I get the target door position:

GameObject targetDoor = newRoom.GetComponent().northDoor;

Then its local X position (the Y position is always the same for all rooms, and the Z position is easy to deter$$anonymous$$e otherwise):

float targetDoorX = targetDoor.transform.localPosition.x;

Then the final X position of the new room:

float targetX = newRoom.transform.position.x-targetDoorX;

And finally move the room (the Y and Z positions are deter$$anonymous$$ed elsewere and are correct):

newRoom.transform.position = new Vector3(targetX, transform.position.y, targetZ);

But the new room's X position is still wrong:

https://dl.dropbox.com/u/7821237/roomposition.jpg

Any idea what I am doing wrong?

avatar image Fattie · Dec 26, 2012 at 06:26 PM 0
Share

(1) Did you get a chance to do exactly what I said here...

"So your Room thing will have four markers, that is, empty game objects, and we'll call them door$$anonymous$$arkerA, door$$anonymous$$arkerB, door$$anonymous$$arkerC, door$$anonymous$$arkerD."

So, you must ADD four new E$$anonymous$$PTY GA$$anonymous$$E OBJECTS to your prefab or model.

Name them door$$anonymous$$arkerA, door$$anonymous$$arkerB, door$$anonymous$$arkerC, door$$anonymous$$arkerD.

They should simply be empty game objects.

in the editor, move them around so they are perfectly on top of the middle of the four doors.

Have you had a chance to do this?

avatar image ivarhill · Dec 26, 2012 at 11:39 PM 0
Share

I did, and it still seemed to have issues. However, after doing some further testing, subtracting the local position of the new door and also adding the position of the first, connecting door seems to result in the correct position. Thanks for your help :)

avatar image Fattie · Dec 27, 2012 at 08:24 AM 0
Share

great stuff !!

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

10 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

Related Questions

GameObject change Position after game started 1 Answer

How to implement a system for "Road-Construction"? 1 Answer

Spawn GameObjects without overlap 1 Answer

Randomly position Instantiated GameObject's 1 Answer

Keep random Instantiation from spawning ontop of other objects 1 Answer


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