- Home /
How do I find out where a child is with javascript
I want to make a parent move to an object when space is pressed and I don't know how to do that, please help
Answer by DaveA · Sep 22, 2013 at 12:07 AM
Use the transform.position on each one as they are in world coordinates
Answer by robertbu · Sep 22, 2013 at 01:06 AM
I'm unsure what part of this problem you are having trouble with. You can fixup the link to the child in the inspector so that the parent has a reference, or you could use Transform.Find() to find a specific child on a known path, or you can search all the children of a transform for a specific child. After you've found the child, it is a bit tricky to move the parent to the child since moving the parent will also move the child. You could do something like:
pos = child.position;
transform.position = Vector3.Lerp(transform.position, pos, Time.deltaTime * speed);
child.pos = pos;
This will move the parent to the child over time with an eased movement. You could also avoid the moving child issue by temporarily breaking the parent/child relationship and reestablishing it when the move is complete.link text
Answer by Gjallanhorn · Sep 22, 2013 at 11:15 AM
To find a child, use the transform.Find() function, then move your child.
var child = transform.Find("NameOfChild");
transform.Translate(child.transform.position);
He wants to move the parent to the child, not the child to the parent.
This code will move the parent to the child position.
Yes, it will immediately translate to the position, but the child will no longer be there. While what he is asking is open to interpretation, I'm assu$$anonymous$$g he wants the child to be at the same position as the parent at the end of the operation.
Your answer
Follow this Question
Related Questions
Update Parent/ Children From Script? 2 Answers
Create multiple instances of an object 2 Answers
Instantiate object to move with other gameObject 0 Answers
accessing a child 1 Answer
Locking child position 1 Answer