- Home /
Be able to grab something and hold on to it.
I am making a game where an integral part of it is that I would be able to pick up cubes and move them to different places, as well as grab ahold of a moving platform from the bottom so that I move with it. One of my ideas (actually the idea that I want), is to be able to make the cube sticky (by adding a joint when it collides), and then stcking it to the underside of the moving platform. However, I don't know how to hold an object in that way. I can't make it a child of the camera, because then when it moves the character and camera won't, but if I make my character be a child of the cube, then I can't move the cube. A javascript would be nice. Thanks in advance.
Answer by Peter G · Jan 07, 2011 at 02:25 AM
Parenting is the easiest way, but you said you cannot use that. Next would probably be simply setting the cubes position to be right in front of your character.
//PlayerController.js private var pickedUpObj : Transform;
function Update () {
//Add code for selecting an object. The easiest way is probably Physics.OverlapSphere()
pickedUpObj.position = transform.position + transform.forward;
//Places the cube 1 unit in front of the player.
}
If your player is controlled by a Rigidbody, then you could add a FixedJoint to your cube and it will stick.
and if I use this, when the cube is forcefully moved, I will be too?
No, sorry, in that case you should probably opt for a Physics controlled character and then use a Fixed or hinge joint. Even still, I don't know if forces on the child rigidbody move the parent. I would guess yes, but you need to test that.