Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Trying to make physics based drag, coming out as buggy and non-functional

Discussion in '2D' started by jukeistiscool, Sep 11, 2023.

  1. jukeistiscool

    jukeistiscool

    Joined:
    Aug 19, 2023
    Posts:
    3
    Hello.
    I am a new unity creator, and right now I am working on a game similar to Google Gravity if you've ever seen that.

    I am trying to make a special type of drag and drop which can pick up parts that have Rigidbody's (for Physics) at specific points on the part by adding a HingeJoint2D to them and trying to set the anchor of it to the part's position + the offset of my mouse when I selected the part.

    So far this hasn't worked and there's another thing that isn't working either.

    Like, being able to throw them around the place (They just ignore sideways force when let go and fall straight down).

    Any help is appreciated!
     
    Last edited: Sep 11, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    A HingeJoint2D isn't something you'd ever use for this; you should use the TargetJoint2D which is actually dedicated to doing it i.e. moving a Rigidbody2D to a specific target position using physics forces. You can control how "strong" this is i.e. springy-dragging or rigid-dragging.

    You can find this in my physics examples (see video description for more details):
     
  3. jukeistiscool

    jukeistiscool

    Joined:
    Aug 19, 2023
    Posts:
    3
    How would I go about writing the code to drag a part using this method then?

    (Not a script for each individual part, instead a script to go on something global that says you can drag on parts with a certain layer named "Draggable")
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Kurt-Dekker likes this.
  5. jukeistiscool

    jukeistiscool

    Joined:
    Aug 19, 2023
    Posts:
    3