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

Distance Joint 2D moves way too slow

Discussion in 'Physics' started by Warblingpants67, May 27, 2021.

  1. Warblingpants67

    Warblingpants67

    Joined:
    Jun 19, 2020
    Posts:
    17
    I'm trying to use a distance joint as a grappling-hook type thing in my platformer game. The problem is that gravity barely seems to effect the player when using the grappling hook, causing the player to not swing and instead very slowly reach the bottom of the hook. Is there any variable I need to change to make it actually function correctly, so that the physics of this hook actually make sense? I'm truly stumped right now.
     
    Last edited: May 27, 2021
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,333
    You have the project in-front of you and you cannot figure it out; imagine how we feel with a vague description of what you're doing.

    No idea what that means. Needs a picture which also indicates what physics bodies are involved in this.

    No idea what this means. I know what a player is and what gravity is but that's it.

    Can you imagine how hard it is to visualise this based upon the previous description you've made?

    Without knowing what the heck is going on, no. I'm sure someone will jump on and tell you to change some setting but it's all guesswork without knowing what it is you want.

    Me too.


    I only highlight the above because we get posts like this so many times each day and it's impossible to actually help because there's an assumption the problem has been explained.
     
    Warblingpants67 likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,333
    If this "grappling hook" needs to essentially drag objects around then you should use the TargetJoint2D. You can find one example of its usage in the GitHub repo in my signature. Here's one example:
     
    Warblingpants67 likes this.
  4. Warblingpants67

    Warblingpants67

    Joined:
    Jun 19, 2020
    Posts:
    17
    @MelvMay Sorry for the terrible explanation, I'll try to explain it better. I have a GameObject that uses Raycasts to find valid surfaces to attach my grappling hook to, and aims towards the mouse. When you click the left mouse button, a Distance Joint 2D on the player (which uses a rigid body, and basic key inputs for movement) is activated, connected to the GameObject and the distance is automatically configured. All of this works fine, except when you are hanging in air from this "grappling hook" gravity doesn't really seem effect the player, causing the character to be stuck in the air at gravity-defying angles, and not swing like you would expect a grappling hook to do. Hopefully this is more clear? I'm having a hard time explaining this, sorry
     
    Last edited: May 27, 2021
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,333
    I don't see how the gravity wouldn't affect the player. A distance joint is just that, a distance contraint; it wouldn't stop gravity or a force affecting a body. Maybe show a video or something.

    Again, take a look at my repo. There's some simple examples of using that joint.
     
    Warblingpants67 likes this.
  6. Warblingpants67

    Warblingpants67

    Joined:
    Jun 19, 2020
    Posts:
    17
    Alright, I recorded a short video. The small square you see is the anchor point, green when I'm just pointing with my mouse and blue when I'm clicking to activate the grappling hook.

     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,333
    The DistanceJoint2D doesn't affect rotation or gravity so something else is being applied to that Rigidbody2D. I just don't know what. Can you duplicate this in a separate project? If so, could you send it to me or DM me with your email and I'll set-up some for you to host it.

    Make sure you've not got stray code modifying the Transform in there or something.
     
    Warblingpants67 likes this.
  8. Warblingpants67

    Warblingpants67

    Joined:
    Jun 19, 2020
    Posts:
    17
    I decided to comment all of my functions one by one, then test to see which ones affected the grappling hook. I discovered the way I was applying my run force caused the velocity to be set to zero when not touching the move buttons. Now that I know the problem, it should be much easier to fix. Thank you for your help!
     
    Last edited: May 29, 2021
    MelvMay likes this.
  9. samvelo00

    samvelo00

    Joined:
    Oct 16, 2021
    Posts:
    1
    hi, i found how you can fix this problem, you should make running script with AddForce instead of velocity

    upload_2021-10-16_20-14-9.png
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,333
    Note that the above isn't particularly good advice simply because you're applying an impulse per-frame with no time-scaling so the force would change with the frame-rate.