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:
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?
Answer by streeetwalker · Sep 29, 2020 at 05:56 PM
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...
Answer by emmajbp · Sep 29, 2020 at 06:33 PM
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: Got:
Sorry, I'm sure I am dense, but your diagram is completely confusing to me when I try to reconcile it with your stated issue.
You state you want "in the opposite direction where the ball is going to be launched" but in your diagram of the "Wanted" you show the mouse point dragging in the same direction as the ball moves, not in the opposite direction.
Then in the "Got" part of your diagram, (first it is oriented in another direction) it looks like the ball didn't move as far as you dragged?
To compound my confusion, you are talking about moving a jointed object. but somehow the beach ball image, to me, doesn't convey anything about joints, so I'm not sure what dragging a jointed object has to do with your problem.
There are several types of joints, and certainly if you drag one object of two that are connected by a specif type of joint, the other object will move. So I can't figure out what the joint has to do with anything.
Sorry, I think I could help, but i hope you understand my confusion.
If all you want to do is drag opposite to the direction you want to launch the ball, and the farther you drag the greater the force uses to launch, that is relatively simple and I have a project I could send you that does exactly that with a soccer ball motif. But it doesn't seem like that is what you want, or is it?
Oh i see the confusion, i took this from the game, maybe its more intuitive?:
The gizmo circle at right is point A and the other one is point B, se how its aligned with the doted trayectory? i want to do the same with the jointed black circle, but se how instead of moving the oposite direction of the trayectory is moving towards the point B position?
what i ineed is to align the black circle position to the oposite of the trayectory:
OK, so what is A, B, and the black dot between them - what do those represent in your problem?
Your answer
Follow this Question
Related Questions
Set Z rotation according to Vector2 1 Answer
Trying to make pointer, but ScreenToWorldPoint wont work 0 Answers
How can I incorporate a Rotation Towards the Mouse Position into this Script? I Tried. 0 Answers
Help with middle mouse panning / dragging 0 Answers
How to know if the mouse has moved 0 Answers