- Home /
Do you know how to move rigidbodies like they are children, whilst allowing them to collide with the level?
Here's my issue:
I have a rigidbody that I want the player to be able to "pick up" by holding down the LMB and then drop by releasing the LMB. I've managed to get this working two ways. The first way is to make the object kinematic and then to parent it to the camera. This works perfectly until I get close to a wall or other game object. The rigidbody being carried just passes right through, like it's nothing. Obviously, this can render the game unplayable if the player happens to drop an important object outside of his area and is otherwise just silly and undesirable.
The other method I tried was to keep the item as a non-kinematic rigidbody and just move it with rigidbody.MovePosition. This also works marvelously well, and has the bonus of allowing the rigidbody to ACTUALLY collide with other the other gameobjects! But the MAIN issue, which is that the rigidbody will eventually pass through the other end of the game object, persists.
I can't figure out a way to let the player carry around an object that can NOT be dropped or just "pushed" onto the opposite side of a wall.
][2]][1]
This is a picture of what's okay: The gameobject is picked up and relocates itself to the center of the player's cursor every time he moves.

And this is what's happening that I DON'T like. As you can see, the carried object is actually right inside the wall, when it shouldn't be. If you were to walk a little closer, it would be right on the opposite side.
[2]: /storage/temp/14571-good.png
Answer by whydoidoit · Aug 21, 2013 at 02:33 PM
When you make it kinematic use OnCollisionEnter to identify when it hits something and either a) return it to the position it was the last frame or b) snap it to the position of the thing it collided with.
I'm not sure how to do either of those. I guess I could store it's transform position at the end of every frame and then recall it if it collides with something, but then the player could get his item "Hooked" on some geometry and then "lose it" by running off.
No clue how I would snap it to the position of the thing it collided with, and I feel like that would end up yielding similar results to using rigidbody.$$anonymous$$ovePosition.
Thanks a lot for the answer, though. I appreciate the help so far.
Well snapping would involve using the contacts information in the collision to adjust the position of the item so that none of it's extents penetrated the edges you want protected. A bit of math, but doable.
Answer by Xtro · Aug 21, 2013 at 02:12 PM
You can do a raycast to determine the distance of the object from the camera. This is for kinematic method.
Your answer