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:
But instead my results are this:
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?