- Home /
Attaching a game object as a child during runtime?
//Put the portal outside of the screen by attaching it to the camera as a chile outside the camera view
transform.position = Camera.main.transform.position;
Hi,
I'm trying to attach a GameObject to a parent prefab Main Camera during runtime. However, I also wanted it to be offset outside of the screen. The code above works at the Main Camera's position but how do I make it offset outside the screen?
Thanks.
Answer by syclamoth · Oct 28, 2011 at 04:37 AM
transform.position = Camera.main.transform.TransformPoint(-Vector3.forward * offsetAmount);
Where offsetAmount is the distance behind the camera you want it to be.
Answer by oxium · Oct 28, 2011 at 05:30 AM
you code doesn't attach your portal to the camera but instead just copy the camera position into your portal object position..
I guess want you want is to set the parent of your portal transform to be your camera transform
check that : http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html
Answer by Ludiares.du · Oct 28, 2011 at 06:10 AM
var offset : Vector3 = 3, 0, 0;
var wanttobeparent : GameObject;
function Update()
{
transform.parent = wanttobeparent.transform;
transform.postion = offset;
}
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
change pivot of parent game object 1 Answer
Properly Rotating Child Objects by Script 1 Answer
Renaming Child Objects during Instantiate? 1 Answer
Accessing variable from parent 1 Answer