Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make realistic drag effect?

Discussion in '2D' started by wechat_os_Qy05WDFsVTQfbHpT5ELNCFQJc, Nov 10, 2019.

  1. wechat_os_Qy05WDFsVTQfbHpT5ELNCFQJc

    wechat_os_Qy05WDFsVTQfbHpT5ELNCFQJc

    Joined:
    Nov 10, 2019
    Posts:
    1
    I'm a new unity player.I'm doing a 2D game recently,And I came up with a realistic drag and drop effect.Like dragging a stick,Hold down one end of the stick and drag,The other end will sag because of gravity.Then drag the stick and hit the wall,The stick will also rotate slightly with the drag point as its fulcrum.

    After several nights of learning and writing various scripts, I found that the mouse was always in the center of the object when I dragged it.I also tried holding down the mouse to add a mouse force relative to the direction of the object,But the object will move away from the mouse and swing a lot.

    Please enlighten me,Thank you very much!
     
  2. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    May want to look into Target Joints or Spring Joints.

    upload_2019-11-10_3-1-27.png
    I kinda tried using a Spring Joint. This is how I configured it.
    I highlighted the
    Anchor in a Red Rectangle, those coordinates should match wherever the player is positioning the mouse on your stick.
    The
    Green Rectangle just highlights the values that should really play with I guess. The way I set it up there gives a very weird behaviour.
    Note however, with that setup, yeah the Anchor should be where the player originally grabbed the stick, but when the player moves their mouse, they are actually moving the object selected in the picture above, that one called "MagicalDrag". Then, the Spring Joint will move the actual stick.

    So another values to play with, in the case of a
    Spring Joint, would be the Stick's Rigidbody2D drags and gravity.
    upload_2019-11-10_3-4-34.png
    If your stick is wobbling violently, you want to increase the Linear Drag, maybe a LOT. But then, it will be barely affected by gravity, so you want to increase the gravity scale. I'll leave that to you I guess.

    However, according to something I read somewhere, Target Joints may be better for this application, but I've never used them so I'm just describing how I'd do it with spring joints.