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

HingeJoint2D attach to rigidbody2d at runtime, no swing???

Discussion in 'Scripting' started by Magnumstar, Jan 9, 2016.

  1. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I'm mostly new to Unity and have questions about how Hinge Joint2D works in reference to run-time anchor assignment. The HingeJoint2D is attached to a cylinder that also has a Circle Collider 2D attached. This is so that I can access the OnTriggerEnter2D event when a object with a trigger collider enters the cylinder. I want this entered object to attach to the hinge and pivot at the top right corner of the sprite. So in essence will create a swinging look. I have some preliminary code that almost gets me there, but the entered object doesn't maintain intertia or momentum and also doesn't move (much at all) or swing when velocity is applied to it. It just sits in the center?

    Does anybody have any ideas how I can get my object to swing on the hinge joint 2D?

    Here's my code

    Code (csharp):
    1.  
    2.     void OnTriggerEnter2D(Collider2D contactObject)
    3.     {
    4.         _rb2d_contactedObject = contactObject.GetComponent<Rigidbody2D> ();
    5.         _hJoint.connectedBody = _rb2d_contactedObject;
    6.  
    7.         Vector2 anchorPoint = new Vector2 (0f, 0f);
    8.         Vector2 connectedAnchorPoint = new Vector2 (0f, 3f);
    9.  
    10.         _hJoint.anchor = anchorPoint;
    11.         _hJoint.connectedAnchor = connectedAnchorPoint;
    12.     }
    13.  
     
  2. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I was able to make this lengthen my anchor Y coordinate on hinge joint, works like a charm now!
     
  3. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    My hingejoint2d is working, however, my object that is connected to the hinge joint swings only from 0 to 90 degrees and 0 to -90 degrees. When I apply force to the object attached (velocity), up to this degree of 90 the sprite only serves to shake back and forth at the end of it's rope if you will. I want this swing to be smooth, no jerky movements.

    More info, the object that is contactObject below has a rigid body with a mass of 1. Whilst swinging on hinge, when I press right arrow the object applies velocity to itself in the right direction and vice versa with left.

    Can you look at my code and help me?

    Code (csharp):
    1.  
    2. void OnTriggerEnter2D(Collider2D contactObject)
    3.     {
    4.         _rb2d_contactedObject = contactObject.GetComponent<Rigidbody2D> ();
    5.         _playerCtrl = _rb2d_contactedObject.GetComponentInParent<PlayerController> ();
    6.         _hJoint.connectedBody = _rb2d_contactedObject;
    7.         _rb2d_hJoint.freezeRotation = false;
    8.  
    9.         Vector2 anchorPoint = new Vector2 (0f, 13f);
    10.         Vector2 connectedAnchorPoint = new Vector2 (0f, 0f);
    11.  
    12.         _hJoint.anchor = anchorPoint;
    13.         _hJoint.connectedAnchor = connectedAnchorPoint;
    14.  
    15.         if (_playerCtrl.swingNut == null)
    16.             _playerCtrl.swingNut = this;
    17.  
    18.         _isSwinging = true;
    19.     }
    20.  
     
    Last edited: Jan 10, 2016
  4. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Well I've applied a upward force to movements in addition to moving left or right while swinging. This does allow the player to swing above the 90 degree threshold. However when the player reaches the top of the swing radius he still shakes back and forward while applying additional force in that direction.
     
  5. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Maybe I can check the angle of the joint as player is swinging, and if angle is past a threshold begin applying force in opposite direction, regardless of player movement input.

    I'm new to unity so I'm very unsure of how much control unity is suppose to give me and how much I may need to intervene. So far it looks like unity takes care of setting up complex actions and you must tweak them to perfection programatically, albeit with the use of an abundance of pressure made library methods and properties. Very .NETIsh.