Drag object from a point in screen to a point in world space

Hello all…!

I’ve been strugling with this from a couple days now so i’m pretty much burned out. My limited (but not completely useless) math knowledge wont let me advance from here, I really hope someone can help me, this is the first time i write a question.

my problem is the following:
i was able to drag an object from point A to a limited distance point B, with the mouse, using spring joints.

The base idea is to use it as a direction hint (The example is clear in the game “Golfing over it”)

Now my problem is that i need to achieve the same result dragging from any point in the screen:
168375-b.png

But instead my results are this:
168376-c.png

The code so far for dragging the spring jointed object is this:

 void Update()
{
    if (isPressed)
    {
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (Vector3.Distance(mousePos, anchor.position) > maxDragDistance)
            rb.position = anchor.position + (mousePos - anchor.position).normalized * maxDragDistance;
            
        else
            rb.position = mousePos;
    }

    if (Input.GetMouseButtonDown(0))
    {
        isPressed = true;
        rb.isKinematic = true;
    }
    else if (Input.GetMouseButtonUp(0))
    {
        isPressed = false;
        rb.isKinematic = false;
    }
}

Could someone please help me with this?

Hi @emmajbp, I have a hunch as to the cause of the problem your facing, but I’m not quite sure from your description - I’m not getting it from your diagram.

Is it that the screen space distance does not seem to visually match the world distance?

If so, there is one kind of simple test you can do that will highlight the issue: does the problem go away if you put the camera in orthographic mode and point it straight down on the world? (like in a 2D game).

If not, then perhaps you mean something other than what I am thinking…

Hi @streeetwalker , i wanted to add another diagram but the page did not alowed me.
I want to make a “Drag and release” mobile game. The player should be able to drag and release to shoot a ball with a force to a direction. This is already done. What i want to do is being able to drag the jointed objet in the oposite direction to give the player a hint of the direction the ball is going to be shot.

The “drag begin - end” are the point in screen where the player taps, the A and B are the points in the game, where the spring anchor is A and the jointed object is B. so you can drag the object from A to B(given a limit radius).

So what i want is to be able to drag the jointed object in the oposite direction where the ball is going to be launched, but instead the jointed object is atracted to the mouse in screen space.

I Hope this can ilustrate it:

Wanted:
168382-a.png
Got:
168383-b.png