Dragged object goes through wall

Google hasn’t answered my question so I’ll try here. Everything works fine but dragged object goes through walls. I have tried many ways to solve this but none of them works. I tried using OnTriggerEnter on walls, Overlapshere on dragged object to detect collision but can’t get any of them work properly. I shortened the code a little so I hope i didn’t remove anything important. Here is the code:

    void OnMouseDown()

    {

        mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
        mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
    }

    private Vector3 GetMouseAsWorldPoint()

    {
        Vector3 mousePoint = Input.mousePosition;
        mousePoint.z = mZCoord;
        return Camera.main.ScreenToWorldPoint(mousePoint);
    }

    void OnMouseDrag()

    {
        if (canpickup)
        {
            rb.MovePosition(GetMouseAsWorldPoint() + mOffset);
            rb.isKinematic = true;
        }

    }
    void OnMouseUp()
    {

        rb.isKinematic = false;

    }

    void Update()
    {
        float minDist = 2;
        float dist = Vector3.Distance(Player.transform.position, draggedobject.position);
        if (dist < minDist)
        {
            canpickup = true;
        }
        else
        {
            canpickup = false;
        }

    }

Pretty simple but can’t understand the physics element here which I think is the problem.

Thanks for help!

Kinematic objects are not affected by forces. Unity uses forces to depenetrate colliding objects. See the problem?

The solution is to not make your object kinematic when you pick it up.

With kinematic off it still goes through walls and object starts glitching when picked up

You’ll probably need to move the object with forces rather than MovePosition. Apply a force that pushes the object towards the desired position from its current position.

This is so strange. Add.force doesn’t work either. I also tried it with this script:

But still same effect: goes through walls. It does collide with other objects and I can hit objects with objects though.

Problem solved. Instead of OnMouseDrag and OnMouseDown I used raycasting and input.getmousebutton. Main key is rigidbody.velocity. Everything is ok now. Thanks for helping!

Hi qqepijackxu, is possible to see your code? I’m struggling with the same issue but can’t find a solution. Thank you