Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by emmajbp · Sep 29, 2020 at 04:29 PM · directionmousepositionvector2draggingspringjoint

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: alt text

But instead my results are this: alt text

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?

c.png (3.6 kB)
b.png (5.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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: alt text Got: alt text


b.png (9.5 kB)
a.png (9.5 kB)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image streeetwalker · Sep 29, 2020 at 06:51 PM 0
Share

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?

avatar image emmajbp streeetwalker · Sep 29, 2020 at 07:25 PM 0
Share

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? alt text



what i ineed is to align the black circle position to the oposite of the trayectory: alt text

avatar image streeetwalker emmajbp · Sep 29, 2020 at 07:28 PM 0
Share

OK, so what is A, B, and the black dot between them - what do those represent in your problem?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

213 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges