- Home /
Question by
unwitty · Aug 09, 2016 at 05:18 AM ·
positioningparent transformparent and childchild-to-gameobject
How do I move both the parent and child to a known position where I want the child to be?
I have two gameobjects. One is the child of the other. These two gameobjects should always retain their relative position to each other, that is to say, they should never move independently from each other.
I know where the child should be, and I'd like to move both the parent and child so that the child is where it should be.
How do I do this? See below for visuals:
skitch.png
(61.1 kB)
Comment
Answer by Arkaid · Aug 09, 2016 at 05:38 AM
An easy way would be to move the parent by an offset distance
public class Child : MonoBehaviour
{
public MoveToTarget(Vector3 position)
{
Vector3 offset = position - transform.position;
transform.parent.position = transform.parent.position + offset;
}
}
Your answer
Follow this Question
Related Questions
,My child zeros out while idle 0 Answers
Prevent Grabbed Object from Clipping 1 Answer
parent child object 1 Answer
Why Deparenting The First Child Changes Position of The Parent 0 Answers