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 Jovy83 · May 13, 2015 at 07:49 AM · positionunity5spawncoordinates

How to spawn GameObject exactly on top of another object?

Working on a 2D game in Unity and I'm having difficulties trying to spawn a block exactly on top of a platform in my game. I don't want the block to look like it's floating or overlapping the platform.

I can't seem to get the calculation correct.

I currently have this code:

 public GameObject blockPrefab;
 public GameObject platform;
 public GameObject dummyBlock;

 // Use this for initialization
 void Start () 
 {
     float platformSize = platform.GetComponent<Renderer>().bounds.size.y;
     float blockSize = dummyBlock.GetComponent<Renderer>().bounds.size.y;
     Debug.Log(platformSize.ToString());


     GameObject test = (GameObject)Instantiate(blockPrefab,
                                               new Vector3(platform.transform.position.x, platform.transform.position.y + blockSize, platform.transform.position.z),
                                               Quaternion.identity);
 }

The problem with this is that it spawns the block in a way that it overlaps the platform which means my calculation for the block's y position is wrong. I also tried adding the platformSize instead of the blockSize and it spawns the block way above the platform.

I'm still getting used to working with Unity's world coordinate system, so yeah. Anybody know how to solve this?

Any help is appreciated!

Comment
Add comment · Show 3
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 AeonIxion · May 13, 2015 at 08:24 AM 1
Share

Not sure if this is right, but I think the transform.position of your platform is at the center of your object. If you then add the blocksize to the Y coordinate, you get the y value of the CENTER of your platform + the blocksize. Which means that the part above the center of your platform isn't accounted for. That's why the block overlaps. So theoretically you need to add half of the platform size to the Y of your factor.

  new Vector3(platform.transform.position.x, platform.transform.position.y + blockSize + platformSize /2, platform.transform.position.z)

I think this will work..

avatar image Jovy83 · May 14, 2015 at 01:46 AM 0
Share

Thanks for the comment. It gave me an idea. In addition to the code you provided, I had to divide blockSize by 2, but I got it working now!

avatar image DwaynePritchett · May 14, 2015 at 02:38 AM 0
Share

Copy your comment as an answer and check as "answered"???

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

The appearance of some prefab appears somewhere else but their collider stays in the good place, that to make? 0 Answers

portal problem 1 Answer

Spawning System 1 Answer

Wrapping the visible world? 1 Answer

Navmesh agent teleporting 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