How to Stop a Game Object from Passing through a Collider while it's being Dragged?

I have game objects with rigid bodies and box colliders attached to them. I also have a script attached to them that allows me to click and drag them around when the mouse button is down.

However, when I click and drag them around and pass through the ground, which also has a rigid body and box collider on it, the game object that I am dragging around passes through the collider.

That’s the problem.

How can you stop the game object that is being dragged around from passing through the ground while it’s being dragged?

The game object should just stop being dragged when it hits the ground, not pass through it.

Any help is appreciated.

Thanks in advance.

Use Rigidbody.MovePosition() to move the Game Objects. If you use, say, Translate() colliders won’t behave like you want them to.

Here is the easy and great solution, its late but worth sharing so…it also answers Why and How…

hope it will be helpful.

If using only Addforce is putting your object through the ground, then something else is wrong. Maybe another part of the script, or some other script is messing up, or the set-up is wrong (triggers? bad layers – but that can’t happen by mistake.) Unless the speeds are crazy high (depends on the command.)

Maybe copy the scene, try just dropping the object on the ground (start it so it falls) and comment out parts of the the script so it just gives a single push/move/whatever.)

Also, the ground doesn’t need a rigidbody – just a collider. A rigidbody won’t hurt (but then the ground will fall,) and can check isKinematic (but then easier just not to have it.)