Quantcast
Channel: Questions in topic: "rope"
Viewing all articles
Browse latest Browse all 194

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

$
0
0
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][1] 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(); hj.connectedBody = gameObject.GetComponent(); // adjust the player's anchor. This is the length of the rope Vector2 playerAnchor = new Vector2(0, (hit.point - playerPos).magnitude); hj.connectedAnchor = playerAnchor; } } } [1]: /storage/temp/48186-hinge-example.gif

Viewing all articles
Browse latest Browse all 194

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>