Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jongibson1 · May 20, 2020 at 01:27 AM · rotationparentchildrenpositioning

Instantiate GameObject to specific position on the Parent

Newbie here. I want to Instantiate a GameObject to a specific position on the Parent. I want to place it at the top of the parent. Can I position it immediately when I instantiate it or do I need to use the transform.position? In either case I don't know how to do it. I also need to figure out how to rotate the child on the parent if you are feeling generous with your time. Also, each child/copy or new instantiated object will scale with each new iteration. I'm trying to build a reverse fractal tree (the branches get bigger over time).

Just a warning - you might cringe at other parts of the code that probably could be written better.


using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform Cube; public GameObject masterTree; public int instanceCounter = 1; public int numCubes = 30; public float scalar = 1.4145f; public float initialScale = 10f; public float angle = 30f; private Transform copy;

 void Start()
 {
 
 }

 private void Update()
 {
     if (instanceCounter <= numCubes)
     {
         if (instanceCounter == 1)
         {
             copy = Instantiate(Cube, new Vector3(0, 0, 0), Quaternion.identity);
             copy.transform.localScale = new Vector3(1f, initialScale, 1f);
             copy.name = "Copy" + instanceCounter;
             copy.transform.parent = masterTree.transform;
             instanceCounter++;
         }

         var copyParent = GameObject.Find("Copy" + (instanceCounter - 1));
         Vector3 copyParentSize = copyParent.GetComponent<Renderer>().bounds.size;
         //Debug.Log("copyParentSizeY = " + copyParentSize.y);
                     
         copy = Instantiate(Cube, new Vector3(0, copyParent.y + copyParentSize.y, 0), Quaternion.identity);
         copy.transform.localScale = new Vector3(1f, initialScale, 1f);
         initialScale = initialScale * scalar;
         copy.name = "Copy" + instanceCounter;

         //copy.transform.rotation *= Quaternion.Euler(angle, angle, 0);
         copy.transform.parent = copyParent.transform;
         instanceCounter++;

       
     }
     {
         
     }
 }

}

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 dahiyabunty1 · May 20, 2020 at 03:40 AM

make it simple... add an empty child gameobject in desired location and then use the transform of that child gameobject

public Transform desireLocation;

Instantiate(Cube, desireLocation.position, Quaternion.identity);

Comment
Add comment · Show 3 · 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 jongibson1 · May 20, 2020 at 03:43 PM 0
Share

Interesting workaround! I like your style. The more I think about it I just want to know how to move instantiated gameobjects around in relation to other instantiated gameobjects. How do I get the location to place that empty child gameobject? Also, how do I add spacing between objects?

For example, I have one instantiated gameobject named Copy1 and another instantiated gameobject called Copy2. I want to move Copy 2 on top of Copy 1 with some kind of spacing, say (0, 5, 0). So, how can I change the position of Copy 2 to be the position of Copy 1 + height of Copy 1 + spacing of 5? I want to be able to know how to do this as a parent/child relationship and as to separate gameobjects.

alt text

screen-shot-2020-05-20-at-114711-am.png (48.2 kB)
avatar image jongibson1 · May 20, 2020 at 03:59 PM 0
Share

To be even more specific, I want to accomplish something like this (see image). I want to be able to scale and move instantiated gameobjects around in relation to each other. I know how to scale a GO, I just can't figure out how to move them around in relation to each other. I need to be able to do this multiple times during the game (not just setting the position when they are first instantiated). alt text

avatar image jongibson1 · May 20, 2020 at 04:24 PM 0
Share

Here's a video of what I'm getting when I parent the objects to each other: http://jonathangibson.com/unity/unity2.m4v I'm using copy.transform.localPosition = new Vector3(0, 1.5F, 0); to change their position.

The spacing is not precise between the children. I think this is because of the scaling? Also I don't understand how hinge points work.

avatar image
0

Answer by jongibson1 · May 20, 2020 at 08:09 PM

I understand Vector3 better now. It looks like I can add together different GameObject positions together in the same Vector3:

 newPosition = new Vector3(0, copyParentSize.y + copyParentPosition.y + spacer, 0);

Now I can move objects around based on the location of other objects. Thank you for your help!

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

167 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 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 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 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 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 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 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 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

Parent rotates around childs location 1 Answer

Rigidbody +parenting +rotation +elevator = Player unwanted behavior(pushed away) 1 Answer

Following another object's position/rotation like parent/child relationship? 4 Answers

Child ignoring parent rotation? 1 Answer

rotate a object around the x axis like other object... stucks at 90 degrees 2 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