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 ahmetegesel · Jan 25, 2014 at 06:22 PM · 2dinstantiateprefabtopdownalign

How to instantiate a gameobject by aligning it to another one

Hello. I'm new in Unity3D. I'm developing a top-down game (2D Game). I have a floor prefab and I want to instantiate it, but its spawn position in YAxis has to be upper edge of previous floor prefab.

What I want to achieve is: For example, I want to construct a 5 floors building.

Here's my GameSrarter script.

using UnityEngine; using System.Collections;

public class GameStarter : MonoBehaviour {

 void Awake(){
     // First prefab is instantiated Vector2.zero position.
     GameObject floor1 = Instantiate(Resources.Load("Prefabs/floor"), Vector2.zero, Quaternion.identity) as GameObject;
     // But I cannot find upper edge of floor1 prefab as Vector.
     GameObject floor2 = Instantiate(Resources.Load("Prefabs/floor"), IDontKnowHowToSpecifyThisOne, Quaternion.identity) as GameObject;
 }

}

Thanks for your helps.

Comment
Add comment · Show 2
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 getyour411 · Jan 25, 2014 at 07:28 PM 0
Share

Could you use floor1.transform.position.y + 2 (or some # that works for what you are trying to do)

avatar image ahmetegesel · Jan 25, 2014 at 11:46 PM 0
Share

I tried it. But floor heights might change. I have to find the exact vector position for the next floor that will be placed at top of the previous floor.

2 Replies

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

Answer by ahmetegesel · Jan 27, 2014 at 03:17 PM

Floor prefab consists of 2 prefabs: "top" and "bottom".

alt text

What I wanted to achieve was: alt text

 void Awake(){
         float nextPosition = Vector2.zero.y;
         for (int i = 0; i < 5; i++) {
             GameObject floor = Instantiate(Resources.Load("Prefabs/Environment/floor"), new Vector2(Vector2.zero.x, nextPosition), Quaternion.identity) as GameObject;
             Transform floorTop = floor.transform.FindChild("top");
             Transform floorBottom = floor.transform.FindChild("bottom");
             float halfBottom = (floorBottom.renderer.bounds.size.y / 2);
             float halfTop = (floorTop.renderer.bounds.size.y / 2);
             nextPosition += ((floorTop.position - floorBottom.position).y) + halfTop + halfBottom;
         }
     }

This code works for me but If anyone knows such efficient way that make this code simple, Please answer.

Thank you.


ekran alıntısı.png (2.5 kB)
ekran alıntısı.png (33.0 kB)
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
avatar image
0

Answer by Kiloblargh · Jan 25, 2014 at 08:04 PM

Presumably your floor prefabs are all different heights? (If not- duh!) So, maybe you want a

 howHigh :    Dictionary.< GameObject, float >

That keeps track of the height of all floors prefabs. And a

 heightSoFar : float 

that is set to zero with each new building, and increased by the appropriate floor height ( howHigh[floor1] ) etc. each time you add a floor to the building.

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 ahmetegesel · Jan 27, 2014 at 08:52 AM 0
Share

How can I find height of the floor and use it to deter$$anonymous$$e start positiom of next floor prefab?

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

18 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

Related Questions

prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers

Instantied 2D Prefab Is Invisible 1 Answer

How to move Instantiated 2D objects by 0.5 using arrows 1 Answer

How to instantiate and destroy objects with 2d trigger 1 Answer

Add force to instantiated prefabs (c#) 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