Place and Move Child with Parent?

I want a Sphere to follow a hand. The sphere is created in script.
My Problem is that the sphere is moving randomly around and not fixing to his parent…
My Code:

Start Method:

GameObject newBall = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Rigidbody rB = newBall.AddComponent<Rigidbody>();
newBall.rigidbody.useGravity = false;
newBall.rigidbody.mass = 0.1f;
Transform tempTrans = newBall.transform;
tempTrans.parent = Hand.transform;
tempTrans.localPosition =  Hand.transform.localPosition;

Update Method:

Hand.transform.GetChild(0).transform.localPosition = Hand.transform.localPosition;

With transform.position; the same weird thing is happening…

4 Answers

4

You don’t need to create your tempTrans Transform, nor do you need update the position of the child object in Update()

Simply set the parent of the newBall to be the Hand.

So…

newBall.transform.parent = Hand.transform;

The rest you can leave out.

If the child is parented to the hand and you want it to remain attached to the hand, I don’t think you should be changing the position of the ball at all.

Just move the hand, and the ball will automagically stay with it.

Try removing this:
Hand.transform.GetChild(0).transform.localPosition = Hand.transform.localPosition;

It sounds like you and Unity may both be doing the same thing ;).

Ah ok, that sounds good. The only problem is, it’s still moving randomly!
I just commented out the Rigidbody and everthing works fine… :confused: is there any possibility to switch it off or deaktivate it? Sleep() and Awake() didn’t work. I could add and remove it but there is no RemoveComponent() Method or is there?

Use [isKinematic][1]. To remove components you use Destroy(). [1]: http://unity3d.com/support/documentation/ScriptReference/Rigidbody-isKinematic.html

–

Hi @Meltdown ,@cmccown ,
I ran into a similar issue where a child should move along with the parent. I tried updating the localPosition of child wrt to parent as well as with updating the position. It works fine in the editor, but not on the phone. I see a lag in the movement of the child.
Any suggestions ?