- Home /
How to hold objects in third person?
I'm trying to make the player be able to hold a ball and be able to carry it.
Can you ad more detail of what part you are struggling with?
$$anonymous$$ake the ball a child of the hand bone and position it accordingly.
Assu$$anonymous$$g you have hand bones you can use:
Transform ball;
Transform handBone;
void Attach () {
ball.parent = handBone;
ball.position = Vector3.zero;
}
Answer by MartinTV · Dec 05, 2018 at 05:57 AM
Pretty simple.
This first method, and the easiest, will only work if the player has a scale of 1 on all axes. Create an Empty Game Object and child it to the player, where you want them to hold the object, and when the player picks up the object you set the object's parent to be the Empty Game Object using the SetParent method.
Then set the position and rotation of the object to be 0 on all axes (0 now being relative to the parent) and it will move to and face the same way as the Empty Game Object. When you want to drop the object you un-child it with:
object.transform.parent = null;
If you don't have a scale of 1 on all axes you don't want to use this method, it could cause the mesh of the object you're picking up to distort due to rotation.
If that's the case then you will want to move the object to that position/rotation every frame without doing the parenting step. If you're objects are Rigidbodies remember to set isKinematic to true so they aren't being affected by gravity while you're trying to hold them.
That should be it. Also it will be the same idea if you have a 1st person game too. Also if you have an animated character, make the Empty Game Object a child of the hand so the object you're trying to hold stays in the hand.