How to fix? Character grabbing system

I wrote this last night, i am trying to recreate the grabbing function of the game little big planet (Those who haven’t played it allows for grabbing of objects on keystroke whether it be a ball to drag around or a rope to swing from)

so everything is functional besides the Hinge im trying to apply and i am not sure why, as it is the first time i’ve ever tried to place a hinge dynamically, any input would be massively appreciated

the script will be applied to the object im trying to grab, if this is isn’t enough information please let me know, i tried the source and google couldn’t make sense of the source and couldn’t find any legitimate guides through google so all i have left is to post and hope.

Script Revision:

public var IsGrabbing : boolean = false;
public var CanGrab : boolean = false;

public var Player : Transform;
public var c : Transform;

function OnTriggerEnter(Other : Collider){
if (Other.gameObject.tag == "Player"){
		CanGrab = true;


function OnCollisionEnter(c : Collision) {
							if (IsGrabbing == true) {
			    var joint = gameObject.AddComponent(HingeJoint);
    joint.connectedBody = c.rigidbody;


function OnTriggerExit(Other : Collider){
if (Other.gameObject.tag == "Player"){
		CanGrab = false;


function Update() {
		if 		 (Input.GetKeyDown(KeyCode.G) && CanGrab == true) {
					IsGrabbing = true;
		if 		 (Input.GetKeyUp(KeyCode.G)) {
					IsGrabbing = false;
		if	(CanGrab == false) {
				IsGrabbing = false;

     if (IsGrabbing == false) {
			     Destroy (gameObject.GetComponent(HingeJoint));


Thanks Chris for the pointers i will have to look into functions more as the only 3 i understand won’t work for Adding the joint or at the very least i need to re examine what i’m trying to achieve

Thanks wibble i was planning to restrict its movement once have some basic functionality so that will be of use in the future, i definitely still have alot to learn because i had no ideas on that matter at all.

EDIT: Ok so i made some changes to the script and its somewhat functional (atleast now i know that the hinge is being placed) i believe i need to look into its constraints though because when i move my character the object doesn’t, i added the revision if anyone has any additional feedback,
Thanks again at the very least for the ideas on what to google and mostly for taking the time to reply.

Hi there

Couple of things:

  • you’re using a fixed joint there, not a hinge joint - I’m guessing you knew that but thought it worth pointing out
  • I’ve tried the procedure you’re doing and it does seem to work. Are you sure you’re trying to add components linking the correct bodies - both obviously have to have rigid bodies and it’d get upset if they didn’t. Try doing a Debug.Log of both object’s rigid bodies and make sure they’re present
  • It looks from your code like you’re trying to re-add the joint every frame (if IsGrabbing==true). This should only be done once, when the grab is first instantiated
  • You will also need to set the anchor and connectedAnchor values (to say where the hinge is), and the axis (to make sure the constraint is operating in the correct direction)

Good luck with it - interesting to see it done using the 3d constraints - we had a 2d engine (operating with collision layers to make it appear 3d across 7 layers) in LBP, so it was more like a simple pin joint. If the hinge doesn’t work well, might be worth looking at unity’s 2d physics.