Is there a way to set the speed and/or max force of Rigidbody2D's .MovePosition?

Hello everyone!

So I implemented a very basic drag and drop system that actually reponds to physics. Is there a way to somehow cap the speed and/or drag force of an object? I looked at

My code sofar is:

private void Awake()
    {
        cam = Camera.main;
    }

    private void OnMouseDown()
    {
        dragOffset = new Vector2( transform.position.x, transform.position.y) - GetMousePos();
    }
    private void OnMouseDrag()
    {
        Vector2 stuff = GetMousePos() + dragOffset;
        this.GetComponent<Rigidbody2D>().MovePosition(stuff);
    }

    Vector2 GetMousePos()
    {
        var mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
        mousePos.z = 0;
        return mousePos;
    }

Is there any way to do the above mentioned things? The unity documentation shows no possible solutions for this.

Wish everyone a nice day/evening!

The body is not moving and there’s no force, you’re just instantly setting a new position (teleporting) so any capping of movement would be down to you.

I would recommend using the TargetJoint2D for this as is demonstrated in my PhysicsExamples2D GitHub Project.

1 Like