How to make object being dragged stop phasing through map borders

How do I make the object being dragged respect the colliders of my game’s borders? Right now I can simply drag the object through them. It does seem that it detects the collision when i put a debug.log in a OnCollisionEnter void, but to no avail. Heres the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragObject : MonoBehaviour
{
    private float mZCoord;
    private Vector3 mOffset;
    private Vector3 lastMousePosition;
    private Vector3 velocity;

    float flingPowerScale = 0.33f;
    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();

        if (rb == null)
        {
            Debug.LogError("No Rigidbody found on the object. Please attach a Rigidbody.");
        }
    }

    private void OnMouseDown()
    {
        mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;

        // Store offset = gameobject mouse pos - mouse world pos
        mOffset = gameObject.transform.position - GetMouseWorldPos();

        // Initialize the last mouse position
        lastMousePosition = GetMouseWorldPos();
    }

    void OnMouseDrag()
    {
        Vector3 currentMousePosition = GetMouseWorldPos();

        // Calculate velocity based on the difference between current and last mouse positions
        velocity = (currentMousePosition - lastMousePosition) / Time.deltaTime;

        // Update position based on mouse drag
        Vector3 newPosition = currentMousePosition + mOffset;

        // Apply the clamped position
        rb.MovePosition(newPosition);

        // Update last mouse position
        lastMousePosition = currentMousePosition;
    }

    private void OnMouseUp()
    {
        // When the mouse is released, apply the calculated velocity to the Rigidbody
        if (rb != null)
        {
            rb.velocity = velocity * flingPowerScale;
        }
    }

    private Vector3 GetMouseWorldPos()
    {
        // pixel coordinates (x,y)
        Vector3 mousePoint = Input.mousePosition;

        // z coordinate of gameobject on screen
        mousePoint.z = mZCoord;

        return Camera.main.ScreenToWorldPoint(mousePoint);
    }

}

The 3D version of MovePosition doesn’t block collisions very well and so you’ll need to drag the rigidbody around by setting its velocity or by using AddForce.

You must move your object with Rigidbody.AddForce exclusively. Also, instead of modifying Rigidbody.velocity, you should use AddForce with ForceMode.VelocityChange.

When using MovePosition, the object will be forcefully moved to the given position. Once it’s there, the physics engine will try to resolve whatever situation it encounters. If the object is interpenetrating other object, it will try to pull them apart. But if it’s too far inside the other object, then the situation may be resolved by putting the object at the other side. That’s how the dragged object can end at the other side of the map borders.

Using AddForce allows the physics engine to detect and resolve the collisions properly.