Carrying physical game objects in javascript scripting problems with Unity 5.

Currently, I’m making a small game in unityscript, and I have a character with the standard first person controller script. Problem is, when I use the following .js script, a object that is being carried goes out of control. Floating around, to spinning in a type of motion like it’s in a invisible tornado. And for the record this is in unity 5.3.3.

Here is the small script.

var onhand : Transform;

function Update() {

}

function OnMouseDown () {
	GetComponent.<Rigidbody>().useGravity = false;
	this.transform.position = onhand.position;
	this.transform.parent = GameObject.Find("FPSController").transform;
	this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
}

function OnMouseUp () {
	this.transform.parent = null;
	GetComponent.<Rigidbody>().useGravity = true;
}

I did try using GetButtonDown as well, with no success. Bare in mind, the GameObject that is suppose to be carried does have a rigidbody. And the player does have one as well.

How can this problem be solved?

Thanks.

From my own experience, one solution I would recommend would be making the rigidbody kinematic. This should prevent it from doing weird stuff, but does have some drawbacks as well. Test out this version and see if it works how you want:

var onhand : Transform;

function OnMouseDown()
{
	GetComponent.<Rigidbody>().useGravity = false;
	GetComponent.<Rigidbody>().isKinematic = true;
	this.transform.position = onhand.position;
	this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
}

function OnMouseUp()
{
	this.transform.parent = null;
	GetComponent.<Rigidbody>().useGravity = true;
	GetComponent.<Rigidbody>().isKinematic = false;
}

As for the errors, are the footstep sounds assigned in the inspector for the First Person Controller Script? If you don’t want them to sound you can always put the Audio Source volume to zero.

Hope this helps!

@ZefanS

Hi again ZefanS , for some reason code sample option has been acting up so doesn’t look right. But when I tried your method the player couldn’t hold on to the object when they let go of the left mouse button.

As for GetButtonDown(again code sample isn’t working in this reply), been there done that, didn’t work.

function Update() {
                  if (Input.GetButtonDown("Press E"))
                                      GetComponent.<Rigidbody>().useGravity = false;
                                       GetComponent.<Rigidbody>().isKinematic = true;
                                       this.transform.position = onhand.position;
                                       this.transform.parent =   
  GameObject.Find("FirstPersonCharacter").transform;
 }

“As for the errors, are the footstep
sounds assigned in the inspector for
the First Person Controller Script? If
you don’t want them to sound you can
always put the Audio Source volume to
zero.”

Did that and still get the same errors.