- Home /
How to move a Child independently to another object and back.
How do I move a Child independently from it's parent to another GameObject and then, after some amount of time, move it back the the position it was with it's parent?
Answer by deltamish · Feb 15, 2014 at 09:12 AM
Hi, There are two ways of doing that
1.By switching its parent back and forth
like the example here
public Transform Parent;
public float WaitTime = 2f;
float timeElapsed = 0f;
void Update()
{
timeElapsed += Time.deltatime;
if(tiomeElapsed >= WaitTime)
{
transform.parent = Parent;
}else{
transform.parent = null;
}
}
2.To calculate the relative pos with respect to world
Whioch is quite difficult.In order to do that uo must calculate distance between parent and this object (at start Call it "dista" ) and then if the parent moves by a certain amount you place this object such that its position is dista times forawrd or backward dep[ending on parent position
Alright, so how exactly can I calculate the distance between the Parent/another Object? I know what you mean by creating "dista", but i've heard of some people using Vector3.Distance(A, B); Would that work?
Hi @Slvr99 "dista" just means distance between the parent and child at start and yes Vector3.Distance(A, B) will work