- Home /
Collision not working properly in a Fixed Joint
I have a 2.5D platformer, where the character puts objects upon his head (objects as big as him) and can move through the scene with them on top of it.
To accomplish that, I put the object (a cube) on its head modifying the cube's rigidbody.position and I create a fixed joint on the character's head with the cube as Connected Body, with enable collision as true (although true o false seems to make no difference with the problem). This is the code:
fixedJoint = anchorPoint.gameObject.AddComponent<FixedJoint> ();
fixedJoint.connectedBody = item;
item.mass = itemMass;
item.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
Where anchorPoint is an empty object, child of the character, with the correct position where the fixedJoint should be and item is the rigidbody of the cube.
And this is the jumping function:
void Jump(){
if (isGrounded && !isDown) {
rigidbody.AddForce(Vector3.up * minJumpForce , ForceMode.Impulse);
isJumping = true;
pointOfOrigin = groundDetector.position;
}
if(groundDetector.position.y - pointOfOrigin.y > maxJump){
isJumping = false;
}
if (isJumping) {
rigidbody.AddForce(Vector3.up * jumpIncrement , ForceMode.Force);
}
}
Which is executed every FixedUpdate while the jump button is pressed.
The problem is that the cube no longer collides with platforms above the character's head. If the character is under a platform and jumps, the cube on his head passes through the collider of the platform from below and remains "inside" the platform until the head hits the collider correctly. Then the character stops and falls with the cube still attached.
Even worst, if the platform height is thin enough to let some part of the cube to reach the other side of the platform collider, the cube teleports to the other side, but can't go down because now the collider does work, stretching somehow the fixed joint. If the character then moves to the left or right, the cube follows him sliding through the platform, and when reaches the end, without the collider in-between, it jumps to his head again, all of it without breaking the fixed joint (with break force set to infinity).
If I make the cube child of the character it also makes no difference. The colliders doesn't "concatenate", the platform stopping the cube from passing through it and the cube stopping the character from reaching the platform, which is what should happen.
The character is moving using the rigidbody (rigidbody.moveposition()) and jumping adding up force to it, so I'm not messing whith the transform at all. I've also tryed setting the collision detection to constant dynamic for the cube and contant for the platform in case that the problem was that the cube was hiting too fast, but no difference, and also the character without the cube is colliding perfectly and is moving at even more speed (because has't to lift the cube mass too in addition of its own).
I'm not sure what I'm doing wrong.