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 /
avatar image
0
Question by carsons · Jun 14, 2015 at 07:29 AM · 2dscripting problemplatformerhingejointrope

How to fix 'jumping' behavior with hinge joint 2D

I am trying to create a rope swinging mechanic for a 2D game. I am using a single hinge joint to act as my rope. I create the joint by script, allowing the user to aim the ropes. The problem is that when the hinge joint is created, the object it is attached to 'jumps'.

Here is a clip showing the problem:

alt text

The left square is the 'player' who is swinging from the rope. When I click, the hinge joint is created (as well as the pink line to help you see), and the player 'jumps' to a new position. Why is this jump happening?

Here is the relevant code from the script:

 void Update () {
 
         if (Input.GetMouseButtonDown (0)) {
 
                         // click location
             Vector2 clickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
                         // player position
             Vector2 playerPos = new Vector2(transform.position.x, transform.position.y);
 
 
             // cast a ray from the player to the mouse click
             RaycastHit2D hit = Physics2D.Raycast(playerPos, clickPos - playerPos, 100, layerMask);
 
             if(hit.collider){
 
                                 // add the hinge joint 
                 GameObject go = hit.collider.gameObject;
                 HingeJoint2D hj = go.AddComponent<HingeJoint2D>();
                 hj.connectedBody = gameObject.GetComponent<Rigidbody2D>();
 
                                 // adjust the player's anchor. This is the length of the rope
                 Vector2 playerAnchor = new Vector2(0, (hit.point - playerPos).magnitude);
                 hj.connectedAnchor = playerAnchor;
             
             }
         }
 }

hinge-example.gif (487.0 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by carsons · Jun 15, 2015 at 05:43 AM

For anyone that gets stuck on this problem, here is the answer:

It turns out that there is a Unity bug that prevents you from properly creating a hinge joint on mouse click. Talked about in answer here. (this says the bug is in Unity 4.6, but I have the same issues in Unity 5).

You must create the hinge joint earlier and enable it on click. Here is an example (this anchors the hinge to where you click, rather than to another GameObject like my original question, but is similar enough):

 // Example from user "Dan" on gamedev.stackexchange.com
 public class AddHingeOnClick : MonoBehaviour {
     HingeJoint2D hingeJoint2D;
 
     void Start () {
         hingeJoint2D = gameObject.AddComponent<HingeJoint2D>();
         hingeJoint2D.enabled = false;
     }
 
     void OnMouseDown() {        
         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         Vector3 mouseLocalPosition = transform.InverseTransformPoint(mouseWorldPosition);
         hingeJoint2D.enabled = true;
         hingeJoint2D.connectedAnchor = mouseWorldPosition;
         hingeJoint2D.anchor = mouseLocalPosition;
     }
 }


Comment
Add comment · Show 1 · 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 TwinPrime · Nov 10, 2017 at 06:08 PM 0
Share

Same problem here....

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make a companion that trails character moves, and not collide? 0 Answers

Why isn't my instance of an object moving? 1 Answer

Ground dash is like a teleport 0 Answers

Can't get a 2d chain to work using hinge joints. 0 Answers

How do I make the length of my rope shorter 1 Answer


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