- Home /
how to merge object same as Parent Child ?
Hi,
I have Object A and Object B. now i want to attached both the ojects. So i have make Object A as parent of Object B .But the problem is that if i move Object B then object A is not moving but if i move Object A then object B moves prefectly. So please any body help me ?
For Drag the object i am using bellow code:
void OnMouseDown ()
{
if (isEnable) {
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
}
void OnMouseDrag ()
{
if (isEnable) {
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
transform.position = curPosition;
}
}
Thanks Yashesh.
That is how a parent-child relationship works. You need to read up on the fundamentals on how Unity works. It's not clear what you are trying to achieve.
If Parent is moving then child is following to it. But if child is moving then parent is not following to it. So i want that parent should follow to child ...
If you want both objects to follow each other, then you should put both objects in the same empty GameObject. $$anonymous$$ake the empty gameObject the parent and Object A and Object B the child.
If you move the empty game Object both A and B will move.
thanks for reply. I will try this and tell you the results.
Answer by mattssonon · Oct 16, 2013 at 12:46 PM
As per our discussion in the comments above, put the script back on the children and change it to this:
void OnMouseDown ()
{
if (isEnable) {
screenPoint = Camera.main.WorldToScreenPoint (transform.parent.gameObject.transform.position);
offset = transform.parent.gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
}
void OnMouseDrag ()
{
if (isEnable) {
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
transform.parent.transform.position = curPosition;
}
}
Now the script moves the parent, empty GameObject when you drag the children.
Your answer
Follow this Question
Related Questions
How to parent an object without losing its rotation 1 Answer
Merging Object 1 Answer
Problem dragging, scaling, moving objects in #scene view with mouse 0 Answers
Find a specific child of a GameObject with the collision data from OnControllerColliderHit 4 Answers
scaling plane gameobject to fit orthographic view iphone unity3d 0 Answers