character_controller_snap_to_ground
If enabled, snap-to-ground will force the character to stick to the ground if the following conditions are met simultaneously:
- At the start of the movement, the character touches the ground.
- The movement has a slight downward component.
- At the end of the desired movement, the character would be separated from the ground by a distance smaller than the distance provided by the snap-to-ground parameter.
If these conditions are met, the character is automatically teleported down to the ground at the end of its motion. Typical usages of snap-to-ground include going downstairs or remaining in contact with the floor when moving downhill.
// Set snap-to-ground to None to disable it.
character_controller.snap_to_ground = None;
// Snap to the ground if the vertical distance to the ground is smaller than 0.5.
character_controller.snap_to_ground = Some(CharacterLength::Absolute(0.5));
// Snap to the ground if the vertical distance to the ground is smaller than 0.2 times the character’s height.
character_controller.snap_to_ground = Some(CharacterLength::Relative(0.2));
/* Configure the character controller when the collider is created. */
// Snap to the ground if the vertical distance to the ground is smaller than 0.5.
commands
.spawn(Collider::ball(0.5))
.insert(KinematicCharacterController {
snap_to_ground: Some(CharacterLength::Absolute(0.5)),
..default()
});
// Snap to the ground if the vertical distance to the ground is smaller than 0.2 times the character’s height
commands
.spawn(Collider::ball(0.5))
.insert(KinematicCharacterController {
snap_to_ground: Some(CharacterLength::Relative(0.2)),
..default()
});
/* Configure snap-to-ground inside of a system. */
fn modify_character_controller_snap_to_ground(
mut character_controllers: Query<&mut KinematicCharacterController>,
) {
for mut character_controller in character_controllers.iter_mut() {
character_controller.snap_to_ground = Some(CharacterLength::Absolute(0.5));
}
}
let characterController = world.createCharacterController(0.01);
// Snap to the ground if the vertical distance to the ground is smaller than 0.5.
characterController.enableSnapToGround(0.5);
// Disable snap-to-ground.
characterController.disableSnapToGround();
Snap-to-ground is configured with r3KinematicCharacterController_SetSnapToGround, which takes whether it is enabled,
and the snapping distance as an R3CharacterLength (see the character offset):
// Set `enabled` (the second argument) to 0 to disable snap-to-ground (the distance is then ignored).
r2KinematicCharacterController_SetSnapToGround(character_controller, 0, (R2CharacterLength){0});
// Snap to the ground if the vertical distance to the ground is smaller than 0.5.
r2KinematicCharacterController_SetSnapToGround(character_controller, 1,
(R2CharacterLength){.value = 0.5, .relative = 0});
// Snap to the ground if the vertical distance to the ground is smaller than 0.2 times the character's height.
r2KinematicCharacterController_SetSnapToGround(character_controller, 1,
(R2CharacterLength){.value = 0.2, .relative = 1});
Snap-to-ground is configured with the snap_to_ground property: either None to disable it, or the snapping
distance as a CharacterLength (see the character offset). It is
enabled by default, with a distance of CharacterLength.relative(0.2):
# Set snap-to-ground to None to disable it.
character_controller.snap_to_ground = None
# Snap to the ground if the vertical distance to the ground is smaller than 0.5.
character_controller.snap_to_ground = rp.CharacterLength.absolute(0.5)
# Snap to the ground if the vertical distance to the ground is smaller than 0.2 times the character’s height.
character_controller.snap_to_ground = rp.CharacterLength.relative(0.2)