Hi,
I’m making a game which plays on a 2d sidescrolling field. There are alot of 3d rigidbody objects in the level which I want the player to manipulate.
When the player is within a certain distance on a rigidbody he should be able to press a button and ‘grab’ the rigidbody so he can move around with it.
I’m certain if I started messing around with it I could get something working, but I would like your advice and or suggestions on how to best implement this.
Some questions I’m struggling with:
- How I can find the nearest rigidbody?
- Would it be better to create a physics joint with the grabbed rigidbody (the player is also a rigidbody), or manually manipulate the objects position?
Any ideas? Suggestions? Has anyone implemented something similar before?
cheers,
Draknir
You should wait at least 24 hours before postin this heh; I dont think there are any 2-day-game competitions going on right now 
Anywho - it really just depends on what the gameplay is like. It might even be best to disable the physics of the object as soon as the player grabs it and then re-enable after he lets go - then just parent and/or move it around as a Kinematic rigidbody.
If it needs to interact with everything then a joint is pretty much the only way to go. I’d probably disable collision between the player and the object though.
Thanks for the reply, I guess I could have waited a few more hours before bumping 
I don’t think I can disable the physics, because the player will still be moving around the game world and will have to collide with other physics objects.
What would be the best kind of joint do you think? Hinge, Spring or a Configurable one?
Is it possible to parent objects using script?
Since its a 2D side scroller you pretty much have to go with the configurable one to achieve constraint-to-game-plane.
Also - by “Disable physics” i meant disable physics only of the object the player is carrying; until you throw it.
As for parenting - yes you can simply do…
transform.parent = someTransformReference
If you are using a Physics joint to join the two objects - this effectively acts as a constraint to keep the object near the player so there is no reason to use parenting.
There are 2 methods:
1st You could raycast from the objects to the player and the one with the shortest distance (which you can set a max distance for using) becomes available for adding as a child to the player obj.
2nd Use colliders as triggers set to the distance you want the object to become “grabbable”, which your grab function checks for onTriggerStay to be true, at which point you can child the object to the player obj and set its transform vectors to follow the player with an offset (so it doesn’t get set to the exact same location as the player obj)
I’d suggest looking at the 3rd person tutorial under where it shows about collectable pickups (they use colliders as triggers)
Regarding your second question, I have been trying to do something similar, and I ‘need’ physics to work so I can’t do everything with kinematics. Simple setups work ok, but things fall apart when trying to do more complicated things.
For example, as already indicated, the easiest way to ‘grab’ an object is to parent it to the main object. But if you want the coupling to be a bit flexible (i.e., such as when hanging from a rope), the you have to use a joint. But I don’t think joints work as you would expect in the pure physics. For example, I have noticed that even hinge joints move lateral to their primary axis, and unless I am missing something, you can’t change the hinge position, for example to simulate a having a weight hanging on a rope that you are lowering. Still looking for an elegant solution to that one.
Also, I am pretty sure I have discovered a bug that creates all kinds of problems when you parent an object to another and both objects have colliders. It seems as if the local transformation of the parented object is incorrect for the colliders and as the main object rotates about, the colliders of the parented object hit the colliders of the main object (even though they should not move relative to each other).
If you decide to go down that path, it would be great if you can report how it worked for you.
One thing that you may also consider is ‘setting’ on object on top of another, letting the colliders and friction keep the objects together; use a sticky surface to make sure they don’t slide.
Good luck.