I have a player character with a non-kinematic rigidbody that is being moved by pushing it along a flat ground with RigidBody.AddRelativeForce(). In the rigidbody's constraints section, I froze the X and Z rotation to keep it from falling over. This movement seems to work pretty well.
Now, there are many boxes on the ground. I made them (static) colliders (no rigidbodies) so they block player movement. The character is supposed to be able to pick up one of those boxes, carry it around, and put it down somewhere else. I want the carried box to collide with boxes on the ground, which is where the problems start. I found two different approaches to do this, but neither gets me all the way.
Approach 1. For pickup, I used SetParent() to link the box transform to the character dynamically (currently testing with setting the box as child of the character in the Editor hierarchy). This gives me the movement of the carried box and the collisions I want because the collider of the box (which was static) now acts as a rigidbody collider. But now I noticed that the rigidbody's rotation constraints are not working anymore, after the box has been made a child of the character game object. When applying force to the character's rigidbody (while carrying the box), it falls over (X-rotation and Z-rotation are not 0 anymore). I tried giving the box its own rigidbody component with the same constraints as the character parent, but this makes the box move independently. Am I doing something wrong here? Why are the constraints being ignored when the box is added as a child?
The character is rotated in a script based on player input with the following line of code, but removing this code still produces the problem, i.e. the character still rotates around X and Z when carrying the box.
transform.RotateAround(transform.position, Vector3.up, deltaRot);
Not sure if it is relevant, but the character is a prefab of an imported (Blender/.blend) mesh. So in the editor hierarchy, the character game object (with rigidbody and collider) has a number of children (with mesh renderers, those have been created by Unity on import of the .blend file) and one child which is the carried box game object.
Approach 2. When the box is picked up, I create (or activate) a Parent Constraint component on the box, that has the character as a source. This seems to be a "cleaner" way than the first option because I am not messing with the game object hierarchy, but again, problems. First, the collider of the box now interacts with the collider of the character, pushing the character when it should be standing still. It seems weird that this would happen, but I can disable this interaction with Physics.IgnoreCollision(). Second, the carried box is still a static collider and therefore doesn't interact with the static colliders on the ground (and the level boundaries), meaning that the character can shove the box through obstacles. I don't know how to handle this. Adding a rigidbody to the box game object makes the carried box move around on its own again (the Parent Constraint is seemingly ignored). I don't want the boxes on the ground to be moved, so I don't want to give them a rigidbody. I could try to dynamically add a collider to the character game object that matches the collider of the carried box, but that seems tricky (the boxes are later supposed to have different sizes and shapes, so a single collider to enable/disable isn't a great solution).
Can one of those approaches work? Is there a better one I haven't considered, yet? Do I have a problem in my design?


