Punch bag with Rigid Body and a Hinge Joint gets stuck in terrain (Unity)

Started by
7 comments, last by lougv22 9 years, 7 months ago

Hi all,

So I have the following gameplay mechanic in my Unity game: the player has to punch or kick a punch bag and it has to swing back and forth. The player is represented by a 3D model with a Rigid Body attached to it (screenshot #1 below). Also, there are Capsule Colliders and Rigid Bodies attached to his Left Arm, Left Hand, Left Leg, Left Foot, Right Arm, Right Hand, Right Leg, and Right Foot (look at screenshot #2).

The punch bag in question is attached to an object named PunchBagFixture. It's basically a cube with a Rigid Body and a Fixed Joint (look at screenshot #3). The Fixed Joint is not attached to any objects and its Break Force and Break Torque are set to Infinity. The punch bag (object Boxing_Pear_1) is attached to that PunchBagFixture via a HingeJoint, and it also has a Rigid Body and a Capsule Collider (screenshot #4).

The problem is that once in a while after punching or kicking the bag, it swings backwards, then it swings forward, bounces off of the player and gets stuck in the terrain (screenshot #5). I don't know how to prevent that. I've tried setting different values for the Mass and Drag parameters of the punching bag's Rigid Body, as well as the Spring and Damper parameters of its Hinge Joint, but nothing helps. I have a collider on the terrain as well and I've tried removing it, with no success either.

What can I do to fix this? Should I make the RigidBody of the punch bag Kinematic, or should I perhaps disable the collision on the player, after he hits the bag, so the punch bag doesn't bounce off of him (although I kind of like that feature because it makes it more realistic)? Or should I do some kind of scripting for the Rigid Body or Hinge Joint?

Thanks.

92976144-f347-4d72-b1d0-3dcd1235c31a_zps

fadf1126-11b3-4f00-bcc0-0689f6d68e29_zps

f2346471-ba32-4a01-9ad6-451c80eae6d5_zps

f505abcc-2ef5-44fb-9ab7-7d60ac760123_zps

a0fd43dc-fc47-49e4-9679-971c6fe4a23a_zps

Advertisement

Not sure if this would help, but have you tried upping the number of iterations for the physics engine? It could be an instability issue? It seems odd to me that the bag is going down to the floor at all, if it's attached to a fixed joint, it shouldn't ever do anything but swing around? So that could be another place where something is going wrong.

If you make the rigid body of the punching bag kinematic then you/Unity would have to set the rigid body transform each simulation time step ( I'm not familiar with Unity, but iirc its uses PhysX for the physics solution and that I'm familiar with. The previous recommendation is a requirement for active kinematic rigid body. ). If you disable collision on the player after the bag get punched its possible the bag will swing back and also penetrate the player geometry. Looking at the last screen shot, its seems like the punch bag fixture has moved as a result of the player punching the bag. If you think about it, it reality, the fixture shouldn't move, only the object attached to the joint should be moving. So I would investigate that setup. Also whenever possible, for object that may generated large forces as a result of contact, I would suggest using CCD ( Continuous Collision Detection ) instead of Discrete.

Not sure if this would help, but have you tried upping the number of iterations for the physics engine? It could be an instability issue? It seems odd to me that the bag is going down to the floor at all, if it's attached to a fixed joint, it shouldn't ever do anything but swing around? So that could be another place where something is going wrong.

Nope, haven't tried that. I didn't even know you could do that. And yea, I know, that's what puzzles me the most. It always happens after it bounces off of the player during the swing back motion, i.e. not while being kicked or punched. I found that if I increase the Drag property of the Punch Bag's ("Boxing_Pear_1") RigidBody to 8 or more and the Damper property of its HingeJoint to 3 or more, the bag bounces much less (which is actually more realistic, in real life punch bags that big barely swing at all when punched or kicked and they almost never swing back enough to come into contact with the person hitting them) so it's less likely to hit the player and hence, get stuck in the ground, but it still happens once in a while.

I forgot to mention that the Punch Bag game object actually is a parent of several smaller objects (such as the Chain, and the "Torso" of the bag) and they have Mesh Colliders attached to them (screenshots #1 and #2 below). Could those be somehow interfering with the Capsule Collider of the parent game object?

Punch_bag_with_MeshCollider_zpsbfa789c5.

Punch_bag_with_MeshCollider_1_zps859546e

If you make the rigid body of the punching bag kinematic then you/Unity would have to set the rigid body transform each simulation time step ( I'm not familiar with Unity, but iirc its uses PhysX for the physics solution and that I'm familiar with. The previous recommendation is a requirement for active kinematic rigid body. ). If you disable collision on the player after the bag get punched its possible the bag will swing back and also penetrate the player geometry. Looking at the last screen shot, its seems like the punch bag fixture has moved as a result of the player punching the bag. If you think about it, it reality, the fixture shouldn't move, only the object attached to the joint should be moving. So I would investigate that setup. Also whenever possible, for object that may generated large forces as a result of contact, I would suggest using CCD ( Continuous Collision Detection ) instead of Discrete.

I think if I increase the Drag and Damper properties, as mentioned in my post above, the bag swings much less and it barely comes into contact with the player. And if, in addition to that, I make it so the player can't get closer to the punch bag, past a certain threshold, then it might be possible to make it so the bag cannot swing back enough to hit him. I may play around with that.

And I'll investigate the FixedJoint set-up as you suggested, plus experiment with Continuous collision.

Thanks.

Yeah, those mesh colliders seem redundant, so probably worth disabling/removing them to see if it makes a difference.

Update: somebody from the Unity message boards suggested setting the isKinematic property for the punch bug fixture to True in order to prevent it from moving, and that seems to be working well. Just thought I'd share with people here in case someone is having a similar issue.

Glad that worked, but...that still seems really odd. If it's truly a fixed joint that's not attached to anything, it should be rooted to its spot in the world, and it shouldn't be moving regardless of the kinematic setting.

Glad that worked, but...that still seems really odd. If it's truly a fixed joint that's not attached to anything, it should be rooted to its spot in the world, and it shouldn't be moving regardless of the kinematic setting.

Yea, I am not really sure what exactly is happening there either. It's still not 100% solid but the issue occurs much less often. I've been experimenting with increasing the mass of the punch bug's RigidBody to 12 and 20 (up from 10) and that makes it swing slower on the way back so it doesn't collide with the player as hard.

The whole collision detection/RigidBody/Fixed/HingeJoint thing in Unity is still fuzzy to me.

And I just double checked and yes, it is a FixedJoint with its Connected Body attribute set to None. Perhaps the fact the punch bug fixture also has a RigidBody somehow messes up the physics? I am not sure at this point.

Which leads me to another question, can anybody recommend a good Unity book that explains this and other topics well, with actual examples?

This topic is closed to new replies.

Advertisement