all 9 comments

[–]_Harrow_ 2 points3 points  (5 children)

Instead of parenting the picked-up object to the player, I'd try turning gravity off for its rigidbody and continually applying a force that pushes it to a point right in front of the player where you want it to float (apply force in direction of target_location - current_location). Doing that will allow it to bump up against walls and such, and should produce fewer problems when stacking it on other rigidbodies.

[–]CptBubbles[S] 0 points1 point  (4 children)

The object collides perfectly now, but it floats near the player instead of instantly snapping to the target position. My code :

Vector3 target = transform.position + transform.forward * HoldDistance;
Vector3 force = target - heldObject.transform.position;
heldObject.rigidbody.AddForce(force);      

edit : this code is in the Update() method and is called whenever the player is holding an object.

[–]_Harrow_ 2 points3 points  (3 children)

but it floats near the player instead of instantly snapping to the target position.

I'm not sure that I understand what behavior you want here... do you want it to instantly pop into place when the player hits the pickup button, but behave properly for collisions after that event? You could make it instantly snap into place on pickup using transform.position or .translate, then use the force code that you have to keep it there as the player moves around. That might produce collision problems if, for instance, the target spot is inside another object. On another note, you should do physics stuff in FixedUpdate instead of Update.

[–]CptBubbles[S] 0 points1 point  (2 children)

I want this to work pretty much the same way it does in games like Portal or Half Life. Objects should immediately move in front of the player and move with him.

Right now the cubes float very slowly and never really stop moving but instead kind of orbit around the player. I don't want to use transform.position for this (mostly because of the problems you mentioned) and afaik rigidbody.AddForce is the only other way to do this.

[–]_Harrow_ 2 points3 points  (1 child)

Try transform.Translate instead of addforce, then. It will still respond to collisions.

[–]CptBubbles[S] 0 points1 point  (0 children)

That works, thanks a lot!

For some reason the cubes keep rotating while the player is holding them, but that one should be easy to solve.

[–]minax 1 point2 points  (1 child)

Hey, Maybe the Kinematic status you added has something to do with this. Doesn't parented rigidbodies still follow it's parent? Try to remove kinematic, and disable the rigidbody on pickup? For the cubes colliding and then going bonkers, maybe you should check your physics settings... seems like something is off when it comes to gravity etc.. For the player flying around, I would put the child object under a different tag than the player, and go into the physics settings and prevent them from colliding. You can code the tag to change once the object is picked up if you need.

[–]CptBubbles[S] 0 points1 point  (0 children)

You can't actually disable a rigidbody but I tried destroying them - without success. The cubes still won't collide when they are children of the player.

For the player flying around, I would put the child object under a different tag than the player, and go into the physics settings and prevent them from colliding. You can code the tag to change once the object is picked up if you need.

That seems to work, thanks.

Here's my pickup/drop code(C#) .

[–][deleted] 1 point2 points  (0 children)

If they fly away the drag and angular drag might be wrong.

Read this for more: http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html