When dragging a rigid body, there will be a phenomenon of flying through the wall (the following is a 2D rigid body, the same is true for 3D rigid bodies). This is because the movement of the rigid body will also trigger the physical collision mechanism, and forcing the coordinates to change will cause problems. Unity It comes with a method MovePosition that allows rigid bodies to move in compliance with physical laws.
Add a 2D collision body and rigid body to the 2D object, and add a collision body to the object to be collided. Note that the Rigidbody2D-Collision Detection of the main object is set to Continuous (real-time monitoring of object collisions), which truly restores the collision scene. The new script is directly mounted to On the main object, put the code