drag and drop rigidbodies

I have problems lifting rigidbodies, drag and then drop them.

I wanna get something like in Halflife → press and hold “e” to lift a box, walk around and rotate with the box, release “e” to drop it.

In my scene there’s an FPC with rigidbody component and a cube with rigidbody tagged with “box”.

I’ve tried making the box a child of my FPC.

boxes = GameObject.FindWithTag("box");
fpp = GameObject.Find("First Person Controller");

if(Input.GetKey ("e")){
boxes.transform.parent=fpp.transform;
}

…but parenting the box doesn’t work with rigidbodies, it won’t move around - the box only rotates.

Then I removed the rigidbody from the box - which works great as long as it doesn’t collide with (and pierce through) walls.

Has someone already made something like this or can someone help me a little?

Have you tried to remove the gravity from the rigidbody?

Regards,
Afonso

http://forum.unity3d.com/viewtopic.php?t=1378

; )

I’ve tried boxes.rigidbody.useGravity=false; as well:

Now whenever the cube collides with a wall, it bounces off and flies away :wink:

And when removing the rigidbody Destroy (boxes.rigidbody) the parenting works great, but it will pierce through walls…

So, for the collisions I need the rigidbody but for the parenting to work, I have to remove it :cry:

I made one improvement in the mean time:

if (Input.GetKey ("e")  (boxes.transform.position.y<2)){


boxes.rigidbody.AddForce(0,13,0);

AddForce looks more like the player gently lifts the object.
But the upward force interacts with the gravity of the object and makes the box move up and down.

So I need addForce for lifting the box, and when it’s in the air it may not bounce off walls but also not go through them.

update: thanks for the link, I’ll go through that !

:smile:

Great - I took that one, replaced the spring joint by a fixed joint but there’s one thing I can’t get to work:

When I grab the box and walk around, the position of the box doesn’t change with the FPC.
How much work is that? Can someone please help me?

I know I somehow have to add the FPC’s position to the position of the joint… but that script doesn’t work:

hit.rigidbody.freezeRotation=true;
	hit.rigidbody.interpolation=RigidbodyInterpolation.Interpolate;
	
	var fpc = transform.parent.position;
	transform.position = fpc + hit.rigidbody.transform.position;
    spring.connectedBody = hit.rigidbody;

That’s the original code from Joachim:

function Update () {
   var spring : SpringJoint = GetComponent(SpringJoint);
   if (!Input.GetButton ("Fire1"))
   {
      if (spring.connectedBody)
      {
         spring.connectedBody.AddForce(transform.TransformDirection(Vector3.forward) * shootForce);
         spring.connectedBody = null;
      }
      return;
   }

   if (spring.connectedBody != null)
      return;

   var hit : RaycastHit;
   if (!Physics.Raycast(Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0.0)), hit, 100))
      return;
   
   if (!hit.rigidbody)
      return;
   
   transform.position = hit.rigidbody.transform.position;
   spring.connectedBody = hit.rigidbody;
   
   transform.localPosition = Vector3(0, 1, 2);
}

Any idea? Pleeeease …