- Home /
[MovePosition() on parent] + [angularVelocity = x on child] = problem. Unity 2D
Hi all I have a problem with making correct movement in my 2D environment: I have a script on parent object that basically does this:
void Update () {
if (child.GetComponent<Rigidbody2D>().angularVelocity == 0 ) {
child.GetComponent<Rigidbody2D>().angularVelocity = 20;
}
}
It gives the child angular velocity, that is fine and dandy
Also i have another script attached to the parent object that pushed the object in a linear way like this:
void Update () {
if (activated && totalPush < 20) {
gameObject.GetComponent<Rigidbody2D>().MovePosition(new Vector2(transform.position.x +2, transform.position.y));
totalPush += 2;
} else {
activated = false;
}
}
I simplified both codes for easy understanding. My problem is that the child is rotating in a constant rotation when staying still, but when the parent starts to move (and of course child object moves with it) the child stops rotating, until parent stops, and then starts to rotate again. printing out child angular velocity to the console in update, gives a constant '20', even the brief moment it is not rotating (the rotation value in transform in the editor is not changing however, hence the absence of rotation)
Any ideas?
EDIT: I'm using Unity 5.4.0b13
EDIT 2: If I'm making the rotation script turn the parent object, then it working correctly (its moves and rotates at the same time)
$$anonymous$$aking child rigidbody kinematic might be helpful. By the way, you should not set angular velocity of a rigidbody directly.
Sorry I forgot to mention, the child is also kinematic. I can't think of any other way of making it have a constant physical rotation (it is a gear in the game, that rotates constantly and pushes/bounces other non-kinematic objects around it with it's outside "teeth")
Answer by LukasSt · Apr 27, 2016 at 01:30 PM
Well guys, I have created a new project to test some things out and I noticed that even in new project the rotation was being jittery on child when parent was moving (but it was at least moving a bit). So I tried different types of rotating the object (moveRotation for instance) and it then stopped rotating at all when parent was moving. I noticed this was happening due to child being kinematic, so i tried making it non-kinematic and attaching HingeJoint2D to child (connecting with the parent), and Ta-da! it started working, but the child kept slowing down when colliding with other non- kinematic objects (because it was non kinematic). Thats when i thought i should use hinge joint's Motor with big force, and it now works perfectly!
TL;DR : make child non-kinematic, connect child with parent with HingeJoint2D, and use Hinge Motors to rotate ignoring other objects (faking the kinematicness)
Your answer
![](https://koobas.hobune.stream/wayback/20220612075639im_/https://answers.unity.com/themes/thub/images/avi.jpg)