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 Feiser · Jun 18, 2015 at 06:15 AM · instantiatechild object

Instantiating an object to the location of child of another object.

I have a number of different platform Prefabs, and each has two empty objects(PointA and PointB) as childs, which are located at each end of the platforms. I tried to Instantiate a platform to PointB of another platform, but it doesn't seem to instantiate at the correct location.Please forgive my ignorance, as I'm still new at this. Here's what my code looks like: public class PlatformSpawn : MonoBehaviour {

     public Transform [] platforms;
     public int platformNumbers = 0;
     private Vector3 nextSpawnLocation;
     private Transform currentPlatform;
     private Transform nextPlatform;
 
     void Start () {
 
         if (platformNumbers < 3) {
             Invoke ("Spawn",0f);
         }
     }
     
     //Spawning Function
     void Spawn () {
         if (platformNumbers == 0) {
             currentPlatform = platforms [Random.Range (0, platforms.Length)];
             nextPlatform = platforms [Random.Range (0, platforms.Length)];
             Instantiate (currentPlatform, new Vector3 (0f, 0f, 0f), Quaternion.identity);
             platformNumbers++;
             nextSpawnLocation = currentPlatform.FindChild ("PointB").transform.position;
             Instantiate (nextPlatform, nextSpawnLocation + nextPlatform.transform.position - nextPlatform.FindChild("PointA").position, Quaternion.identity);
             platformNumbers++;
             print (nextSpawnLocation);
             print (currentPlatform.FindChild ("PointB").transform.position);
         }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Jun 18, 2015 at 03:36 PM

Looks like you don't know about localPosition, and the math is backwards?

This (edited to length) line in Instantiate:

nextSpawnLocation+nextPlatform.position-nextPlatform.FindChild("PointA").position

looks like you're trying to use PointA as an offset. "Spawn at nextLocation, but shifted by PointA." An easier way to get that is to use localPosition instead of position. Otherwise, the math seems backwards. Shouldn't it be pointA-platformPosition?

If things rotate, then the two are different. LocalPosition is in the parent's local coords, whereas child.pos-parent.pos is always in global coords. But only if parents are rotated, does this matter.

I'm also a little confused by "trying to Instantiate at point B." You can just use pointB.position directly. Even for children, position is the actual, world position. LocalPosition is the one that changes up if you're a child. But if that was what you really wanted, you wouldn't have used all that math?

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 Feiser · Jun 19, 2015 at 12:00 AM 0
Share

Hi Owen, thank you for your answer. Yes, I am trying to use PointA as an offset. I have tried using localPosition to get the distance between the parent object and PointA (child) and add it to where I want to spawn the next platform. However, it seems like its giving the same results. I have also tried changing the math to pointA - platformPosition to no avail. One thing I found weird after trying to figure out the problem, is that when I look in the inspector for the position of the children in the platform object(parent), the positions are irrelevant to the parent's position, despite the description of the inspector saying its displaying the object's localPosition. I wonder if this explains why the platforms are not instantiating in the position I want.

avatar image
0

Answer by Feiser · Jun 19, 2015 at 06:32 PM

After a bit of testing, I have found the answer. The reason why the platforms are instantiating at a spot different than what I intended was because I thought the parentObeject's position is the centre point of the parentObject and all its children (that's what it shows in the game scene when I select the parentObject). When actually, the position of the parentObject is basically the centre point of just the parentObject by itself (without the children). Forgive my naivety.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

find/destroy camera in instantiated object 2 Answers

Issue with local and world positioning 1 Answer

Instantiating a new gameObject as a child of a different gameObject 2 Answers

Instantiate On Container Child 1 Answer

Checking if object intersects? 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