Physics Hand Skeleton
Last updated
Last updated
PhysicsHandSkeleton is a special type of HandSkeleton that is optimized for physics. It moves its hand bones to target poses during the physics update phase (fixed step phase). The logic for actual pose update is handled by its HandBones - PhysicsHandBones. PhysicsHandSkeleton supports only PhysicsHandBone derived bones and different types of PhysicsHandBones can be added to it.
Before setting up a physics hand, some prerequisites should be performed. There should be some kind of XR rig with one or more camera objects in the scene (such as Unity engine-provided XR Rig) and at least one XRHandInputDataProvider - you can find a prefab of one in Prefabs\Components\Input\OpenXR under the OctoXR package root folder. There are two prefabs in that folder, XRHandInputProvider_Left and XRHandInputProvder_Right - one for the left and one for the right hand input. You can place both in the scene, it does not matter where you place them, they can be root objects or a child object of some other. Once you have added a XRHandInputDataProvider you can optionally assign XR Rig object to XRHandInputDataProvider's Input Root field using its inspector panel. This is not required, but it is recommended.
The easiest way to add PhysicsHandSkeleton to a scene is to drag and drop it in the scene from a prefab located in Prefabs\Components\Physics, either RigidBodyHand_Left or KinematicRigidBodyHand_Left for the left hand or RigidBodyHand_Right or KinematicRigidBodyHand_Right for the right hand. Prefab should be dropped in the scene either as a root GameObject or as a child of any object that will never be moved, i.e. it will never have its position, rotation and scale changed - this is important in order for PhysicsHandSkeleton to behave as intended. Once you have added the prefab to the current scene, you will want to assign a pose provider to the PhysicsHandSkeleton, you will find PhysicsHandSkeleton component attached to the GameObject named 'Root' under the prefab root object. All prefabs have default settings and colliders set up and ready.
There are two prefabs for each hand, KinematicRigidBodyHand and RigidBodyHand. The key difference between them is that one is set up to use kinematic rigid bodies and the other one uses dynamic rigid bodies connected with physics joints and both are configured to move their bones to target poses in a way that supports such setup. You are encouraged to try out each one of them in a scene where there are any number of different physics objects present and take note of different behaviours they exhibit when in contact with different objects and when being moved around.
Setting up a PhysicsHandSkeleton manually is basically no different from setting up any other type of HandSkeleton which is covered in detail in Building a HandSkeleton section of the page dedicated to HandSkeleton. There are some rules to follow when manually setting up a PhysicsHandSkeleton however. PhysicsHandSkeleton can have multiple different types of PhysicsHandBones and not all of them will behave the same under the same conditions, so some rules may not necessarily apply to all of them, but in order to ensure the correct behaviour regardless of the types of PhysicsHandBones in the PhysicsHandSkeleton it is best to follow these:
To which ever GameObject you are attaching the PhysicsHandSkeleton, make sure it is not the GameObject whose Transform gets modified by some other system or script, directly or indirectly, by your code or any other. This applies to the whole hierarchy upwards from that GameObject too - it should not have a parent/ancestor object that gets moved around
The previous rule applies to PhysicsHandBone as well, i.e. to the GameObject you choose to attach one of the specific PhysicsHandBone derived scripts
PhysicsHandBones should not be parents/children to each other, nor should they be children of the PhysicsHandSkeleton they are/will be added to. This may seem somewhat counterintuitive, but its best to stick to this in order to avoid any surprises - take a look at one of the previously mentioned physics hand skeleton prefabs and notice the structure they are using
Following the previous recommendations you should be able to have a functioning physics enabled hand skeleton without too much effort on your part.
One important thing should also be mentioned: PhysicsHandSkeleton requires the PhysicsHandBone be attached to the same GameObject it is attached to. PhysicsHandBone is an abstract class, so attaching the PhysicsHandSkeleton to a GameObject will result in error from Unity editor if the GameObject in question does not have a PhysicsHandBone attached already. You will have to manually attach a specific PhysicsHandBone derived script to the GameObject first and then attach the PhysicsHandSkeleton to the same GameObject. PhysicsHandBone that was first attached will then be root bone of the skeleton.
The page after this one describes different types of PhysicsHandBones you can choose from when attaching a PhysicsHandSkeleton to a GameObject.
PhysicsHandSkeleton does not have any additional properties exposed in its inspector panel on top of the ones already exposed by the base HandSkeleton it is derived from. There are just some additional helper functionalities which are specific to the PhysicsHandSkeleton for selecting certain objects in the scene that relate to the skeleton, but that is all there is to it. Most properties that might be of an interest are defined by specific PhysicsHandBones as well as all the Rigidbody and/or Joint components that are required by them.