- Home /
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++;
}
{
}
}
}
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);
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.
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).
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.
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!
Your answer
Follow this Question
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