How does child's rigid body affect parent's rigidbody?
I had a player with such hierarchy:
where Player is a body, Weapon is just an EmptyObject that represents a pivot point for Barrel which is cylinder. So this is how it looks like: Player and Barrel had their own rigidbodyes and raise of Barrel was controlled by script attached to Player:
private void FixedUpdate()
{
Raise();
}
private void Raise()
{
var raiseValue = m_RaisingValue * m_WeaponRaisingSpeed * Time.deltaTime;
if ((raiseValue + m_WeaponTransform.eulerAngles.x) <= m_BotAngelConstraint || (raiseValue + m_WeaponTransform.eulerAngles.x) >= m_TopAngelConstraint)
{
m_WeaponTransform.localRotation = Quaternion.Euler(raiseValue + m_WeaponTransform.eulerAngles.x, 0f, 0f);
}
}
The problem was that Barrel's collider didn't work and could pass thru walls. Then I read that it' bad practice to have rigidbodyes on a parent and a child objects and deleted Barrel's rigidbody. It solved the problem with the collider but changed behavior of Player. Now after some movement it starts to change it's rotation despite it has constraints to freeze rotation on X and Z axis in it's rigidbody, and the Barrel's raise became uncontrollable, it doesn't stay still when I don't control it and Barrels's end strives to fall down.
The question is how to make Barrel to stay still when I don't control it and Player not to rotate on it's fixed axis?
I can't say I fully understand your problem, but if you only ever change your character's position, and rotation with a script - in other words, you don't use velocity or rotation velocity of rigidbody - it might be a good idea to set the whole rigidbody to kinematic.
It's a good option and I even had tried it, and everything was working fine, but I need it to be non kinematic because otherwise it couldn't jump and bounce off each other.
Answer by LeonardNS · Oct 08, 2018 at 10:48 AM
What about rotation? Do you use the rigidbody rotation or only update rotation via scripts? As far as I know, if you lock the rotation on the y-axis as well you get the best of both worlds. Your rotation problem should be solved and they can still bounce off each other. Rotation in collisions is still gone, but that is quite difficult to keep track of anyway.
EDIT: Converted to answer
I locked the rotation on the y-axis on players rigidbody and it's solved both problems! But I still don't understand how does it work. $$anonymous$$y assumption: y constraint on parent's rigid body locked the rotation on the y-axis on the child (Weapon) which now can't go up and down freely and became controllable. Am I right? So the question is, how to lock child's freely rotation without y-axis constraint on the parent's rigidbody and without adding rigidbody to the child?
The problem you had with the barrel sounds really bizarre. I have no idea how locking the y rotation solved it.
Your answer
Follow this Question
Related Questions
Rotating rigidbody problem 0 Answers
Question About NavMesh Agent and rotation 1 Answer
How do I get the parent to follow the child's movement? 0 Answers
gravity is not working 1 Answer
[VR] Rigidbody.AddForce() following controllers rotation 0 Answers